bigbluebutton-Github/bigbluebutton-html5/imports/startup/client/auth.js

107 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-03-11 02:33:46 +08:00
import Auth from '/imports/ui/services/auth';
2017-04-28 21:47:07 +08:00
import { logClient } from '/imports/ui/services/api';
2017-03-11 02:33:46 +08:00
2017-04-27 21:43:21 +08:00
// disconnected and trying to open a new connection
const STATUS_CONNECTING = 'connecting';
2017-03-11 02:33:46 +08:00
export function joinRouteHandler(nextState, replace, callback) {
const { sessionToken } = nextState.location.query;
console.log(`sessionToken=${sessionToken}`);
if (!nextState || !sessionToken) {
replace({ pathname: '/error/404' });
callback();
2017-03-11 02:33:46 +08:00
}
// use enter api to get params for the client
const url = `/bigbluebutton/api/enter?sessionToken=${sessionToken}`;
fetch(url)
.then(response => response.json())
.then((data) => {
2017-07-15 00:59:02 +08:00
const { meetingID, internalUserID, authToken, logoutUrl } = data.response;
2017-07-19 20:44:47 +08:00
Auth.set(meetingID, internalUserID, authToken, logoutUrl, sessionToken);
replace({ pathname: '/' });
callback();
});
2017-06-03 03:25:02 +08:00
}
export function logoutRouteHandler(nextState, replace, callback) {
Auth.logout()
2017-06-03 03:25:02 +08:00
.then((logoutURL) => {
window.location = logoutURL || window.location.origin;
callback();
})
2017-07-15 00:59:02 +08:00
.catch(() => {
replace({ pathname: '/error/500' });
callback();
});
2017-06-03 03:25:02 +08:00
}
2017-03-11 02:33:46 +08:00
2017-07-15 00:59:02 +08:00
/**
* Check if should revalidate the auth
* @param {Object} status
* @param {String} lastStatus
*/
export function shouldAuthenticate(status, lastStatus) {
return lastStatus != null && lastStatus === STATUS_CONNECTING && status.connected;
}
/**
* Check if the isn't the first connection try, preventing to authenticate on login.
* @param {Object} status
* @param {string} lastStatus
*/
export function updateStatus(status, lastStatus) {
return status.retryCount > 0 && lastStatus !== STATUS_CONNECTING ? status.status : lastStatus;
}
function _addReconnectObservable() {
let lastStatus = null;
Tracker.autorun(() => {
lastStatus = updateStatus(Meteor.status(), lastStatus);
if (shouldAuthenticate(Meteor.status(), lastStatus)) {
Auth.authenticate(true);
lastStatus = Meteor.status().status;
}
});
}
2017-03-11 02:33:46 +08:00
export function authenticatedRouteHandler(nextState, replace, callback) {
const credentialsSnapshot = {
meetingId: Auth.meetingID,
requesterUserId: Auth.userID,
requesterToken: Auth.token,
};
2017-03-11 02:33:46 +08:00
if (Auth.loggedIn) {
callback();
}
2017-04-27 03:56:29 +08:00
_addReconnectObservable();
2017-04-28 21:47:07 +08:00
2017-03-11 02:33:46 +08:00
Auth.authenticate()
.then(callback)
2017-06-03 03:25:02 +08:00
.catch((reason) => {
logClient('error', {
error: reason,
method: 'authenticatedRouteHandler',
2017-05-19 07:16:17 +08:00
credentialsSnapshot,
});
// make sure users who did not connect are not added to the meeting
// do **not** use the custom call - it relies on expired data
2017-07-15 00:59:02 +08:00
Meteor.call('userLogout', credentialsSnapshot, (error) => {
if (error) {
console.error('error');
}
});
2017-04-18 01:14:31 +08:00
replace({ pathname: `/error/${reason.error}` });
2017-03-11 02:33:46 +08:00
callback();
});
2017-06-03 03:25:02 +08:00
}