diff --git a/imports/api/custom-users.js b/imports/api/custom-users.js index c7e5a868..812d66cc 100644 --- a/imports/api/custom-users.js +++ b/imports/api/custom-users.js @@ -26,70 +26,72 @@ if (Meteor.isServer) { Meteor.methods({ 'users.invitationLogin': function (code) { - // Reusable matcher object for the next mongo queries - const codeMatcher = { - receivedInvites: { - $elemMatch: { - accessToken: code + if (Meteor.isServer) { + // Reusable matcher object for the next mongo queries + const codeMatcher = { + receivedInvites: { + $elemMatch: { + accessToken: code + } } } - } - // Finding the user for this invite code (and checking whether one-click login is still allowed for it) - const invitedUser = Meteor.users.findOne(Object.assign({ - 'profile.isLimited': true - }, codeMatcher), { - fields: Object.assign({ - emails: 1 - }, codeMatcher) - }) - if (!invitedUser) { - console.log('The code is invalid or login is required first') - throw new Meteor.Error('The code is invalid or login is required first') - } + // Finding the user for this invite code (and checking whether one-click login is still allowed for it) + const invitedUser = Meteor.users.findOne(Object.assign({ + 'profile.isLimited': true + }, codeMatcher), { + fields: Object.assign({ + emails: 1 + }, codeMatcher) + }) + if (!invitedUser) { + console.log('The code is invalid or login is required first') + throw new Meteor.Error('The code is invalid or login is required first') + } - // Track accesses - AccessInvitations.upsert({ - userId: invitedUser._id, - unitId: invitedUser.receivedInvites[0].unitId - }, { - $set: { + // Track accesses + AccessInvitations.upsert({ userId: invitedUser._id, unitId: invitedUser.receivedInvites[0].unitId - }, - $push: { - dates: new Date() - } - }) + }, { + $set: { + userId: invitedUser._id, + unitId: invitedUser.receivedInvites[0].unitId + }, + $push: { + dates: new Date() + } + }) - // Keeping track of how many times the user used this invitation to access the system - Meteor.users.update({ - _id: invitedUser._id, - 'receivedInvites.accessToken': code - }, { - $inc: { - 'receivedInvites.$.accessedCount': 1 - } - }) - console.log(`${invitedUser.emails[0].address} is using an invitation to access the system`) + // Keeping track of how many times the user used this invitation to access the system + Meteor.users.update({ + _id: invitedUser._id, + 'receivedInvites.accessToken': code + }, { + $inc: { + 'receivedInvites.$.accessedCount': 1 + } + }) + console.log(`${invitedUser.emails[0].address} is using an invitation to access the system`) - // Resetting the password to something new the client-side could use for an automated login - const randPass = randToken.generate(12) - Accounts.setPassword(invitedUser._id, randPass, {logout: true}) + // Resetting the password to something new the client-side could use for an automated login + const randPass = randToken.generate(12) + Accounts.setPassword(invitedUser._id, randPass, {logout: true}) - const invitedByDetails = (() => { - const { emails: [{ address: email }], profile: { name } } = - Meteor.users.findOne(invitedUser.receivedInvites[0].invitedBy) + const invitedByDetails = (() => { + const { emails: [{ address: email }], profile: { name } } = + Meteor.users.findOne(invitedUser.receivedInvites[0].invitedBy) + return { + email, + name + } + })() return { - email, - name + email: invitedUser.emails[0].address, + pw: randPass, + caseId: invitedUser.receivedInvites[0].caseId, + invitedByDetails } - })() - return { - email: invitedUser.emails[0].address, - pw: randPass, - caseId: invitedUser.receivedInvites[0].caseId, - invitedByDetails } }, 'users.updateMyName': function (name) { diff --git a/imports/state/epics/fetch-invitation-credentials.js b/imports/state/epics/fetch-invitation-credentials.js index 603b1dfb..f67a646a 100644 --- a/imports/state/epics/fetch-invitation-credentials.js +++ b/imports/state/epics/fetch-invitation-credentials.js @@ -18,7 +18,7 @@ export const fetchInvitationCredentials = action$ => action$ .ofType(FETCH_INVITATION_CREDENTIALS) .switchMap(({code}) => { const meteorResult$ = new Subject() - Meteor.call('users.invitationLogin', code, (error, {email, pw, caseId, invitedByDetails}) => { + Meteor.call('users.invitationLogin', code, (error, result) => { if (error) { meteorResult$.next({ type: ERROR_INVITATION_CREDENTIALS, @@ -27,6 +27,7 @@ export const fetchInvitationCredentials = action$ => action$ meteorResult$.complete() return } + const { email, pw, caseId, invitedByDetails } = result meteorResult$.next({ type: LOGIN_INVITATION_CREDENTIALS })