2017-02-24 02:15:13 +08:00
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
2017-10-12 10:00:28 +08:00
|
|
|
import Users from '/imports/api/users';
|
2020-02-07 04:47:28 +08:00
|
|
|
import userJoin from './userJoin';
|
2017-10-12 09:02:23 +08:00
|
|
|
|
2018-02-20 22:21:51 +08:00
|
|
|
const clearOtherSessions = (sessionUserId, current = false) => {
|
|
|
|
const serverSessions = Meteor.server.sessions;
|
|
|
|
Object.keys(serverSessions)
|
|
|
|
.filter(i => serverSessions[i].userId === sessionUserId)
|
|
|
|
.filter(i => i !== current)
|
|
|
|
.forEach(i => serverSessions[i].close());
|
|
|
|
};
|
|
|
|
|
2017-10-12 09:02:23 +08:00
|
|
|
export default function handleValidateAuthToken({ body }, meetingId) {
|
|
|
|
const { userId, valid, waitForApproval } = body;
|
2017-02-24 02:15:13 +08:00
|
|
|
|
|
|
|
check(userId, String);
|
2017-10-12 09:02:23 +08:00
|
|
|
check(valid, Boolean);
|
|
|
|
check(waitForApproval, Boolean);
|
2017-02-24 02:15:13 +08:00
|
|
|
|
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
userId,
|
2018-06-15 04:24:55 +08:00
|
|
|
clientType: 'HTML5',
|
2017-02-24 02:15:13 +08:00
|
|
|
};
|
|
|
|
|
2017-03-08 01:18:02 +08:00
|
|
|
const User = Users.findOne(selector);
|
|
|
|
|
2017-03-11 02:33:46 +08:00
|
|
|
// If we dont find the user on our collection is a flash user and we can skip
|
|
|
|
if (!User) return;
|
2017-03-08 01:18:02 +08:00
|
|
|
|
2017-10-12 09:02:23 +08:00
|
|
|
// Publish user join message
|
2020-03-12 04:40:50 +08:00
|
|
|
if (valid && !waitForApproval && !!User.connectionId) {
|
2019-02-14 03:44:21 +08:00
|
|
|
Logger.info('User=', User);
|
2017-10-12 09:02:23 +08:00
|
|
|
userJoin(meetingId, userId, User.authToken);
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:15:13 +08:00
|
|
|
const modifier = {
|
|
|
|
$set: {
|
2017-10-12 09:02:23 +08:00
|
|
|
validated: valid,
|
|
|
|
approved: !waitForApproval,
|
2019-01-14 21:23:35 +08:00
|
|
|
loginTime: Date.now(),
|
2019-02-27 01:40:01 +08:00
|
|
|
inactivityCheck: false,
|
2017-02-24 02:15:13 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const cb = (err, numChanged) => {
|
|
|
|
if (err) {
|
|
|
|
return Logger.error(`Validating auth token: ${err}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numChanged) {
|
2017-10-12 09:02:23 +08:00
|
|
|
if (valid) {
|
2018-02-20 22:21:51 +08:00
|
|
|
const sessionUserId = `${meetingId}-${userId}`;
|
|
|
|
const currentConnectionId = User.connectionId ? User.connectionId : false;
|
|
|
|
clearOtherSessions(sessionUserId, currentConnectionId);
|
2017-02-24 04:16:10 +08:00
|
|
|
}
|
|
|
|
|
2017-12-19 20:19:47 +08:00
|
|
|
return Logger.info(`Validated auth token as ${valid} user=${userId} meeting=${meetingId}`);
|
2017-02-24 02:15:13 +08:00
|
|
|
}
|
2017-02-24 04:16:10 +08:00
|
|
|
|
2017-10-12 09:02:23 +08:00
|
|
|
return Logger.info('No auth to validate');
|
2017-02-24 04:16:10 +08:00
|
|
|
};
|
|
|
|
|
2017-10-12 09:02:23 +08:00
|
|
|
Users.update(selector, modifier, cb);
|
|
|
|
}
|