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) {
|
2017-06-09 23:56:30 +08:00
|
|
|
const { sessionToken } = nextState.location.query;
|
|
|
|
console.log(`sessionToken=${sessionToken}`);
|
|
|
|
|
|
|
|
if (!nextState || !sessionToken) {
|
2017-03-17 22:23:00 +08:00
|
|
|
replace({ pathname: '/error/404' });
|
|
|
|
callback();
|
2017-03-11 02:33:46 +08:00
|
|
|
}
|
2017-03-17 22:23:00 +08:00
|
|
|
|
2017-06-09 23:56:30 +08:00
|
|
|
// Auth.set(meetingID, userID, authToken); // TODO
|
|
|
|
// replace({ pathname: '/' });
|
2017-04-27 03:56:29 +08:00
|
|
|
callback();
|
2017-03-17 22:23:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2017-05-10 04:04:30 +08:00
|
|
|
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-03-17 22:23:00 +08:00
|
|
|
.catch(reason => {
|
2017-05-10 04:04:30 +08:00
|
|
|
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
|
|
|
}
|