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

97 lines
2.5 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
}
// Auth.set(meetingID, userID, authToken); // TODO
// replace({ pathname: '/' });
2017-04-27 03:56:29 +08:00
callback();
};
export function logoutRouteHandler(nextState, replace, callback) {
const { meetingID, userID, authToken } = nextState.params;
Auth.logout()
.then(logoutURL => {
window.location = logoutURL || window.location.origin;
callback();
})
.catch(reason => {
replace({ pathname: '/error/500' });
callback();
});
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)
.catch(reason => {
logClient('error', { error: reason, method: 'authenticatedRouteHandler', 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
Meteor.call('userLogout', credentialsSnapshot, (error, result) => {
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-04-27 03:56:29 +08:00
function _addReconnectObservable() {
let lastStatus = null;
Tracker.autorun(() => {
2017-04-27 21:43:21 +08:00
lastStatus = updateStatus(Meteor.status(), lastStatus);
2017-04-27 03:56:29 +08:00
2017-04-27 21:43:21 +08:00
if (shouldAuthenticate(Meteor.status(), lastStatus)) {
Auth.authenticate(true);
2017-04-27 03:56:29 +08:00
lastStatus = Meteor.status().status;
}
});
}
/**
* Check if should revalidate the auth
* @param {Object} status
* @param {String} lastStatus
*/
2017-04-27 21:43:21 +08:00
export function shouldAuthenticate(status, lastStatus) {
return lastStatus != null && lastStatus === STATUS_CONNECTING && status.connected;
2017-04-27 03:56:29 +08:00
}
/**
* Check if the isn't the first connection try, preventing to authenticate on login.
* @param {Object} status
* @param {string} lastStatus
*/
2017-04-27 21:43:21 +08:00
export function updateStatus(status, lastStatus) {
return status.retryCount > 0 && lastStatus !== STATUS_CONNECTING ? status.status : lastStatus;
2017-04-27 03:56:29 +08:00
}