Skip to content

Commit

Permalink
fix: error dialog now shown properly if the invited is registered #297
Browse files Browse the repository at this point in the history
  • Loading branch information
nbiton committed Jun 20, 2018
1 parent 598dd8a commit 67494db
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
108 changes: 55 additions & 53 deletions imports/api/custom-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion imports/state/epics/fetch-invitation-credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +27,7 @@ export const fetchInvitationCredentials = action$ => action$
meteorResult$.complete()
return
}
const { email, pw, caseId, invitedByDetails } = result
meteorResult$.next({
type: LOGIN_INVITATION_CREDENTIALS
})
Expand Down

0 comments on commit 67494db

Please sign in to comment.