From a13b1c95d65ee15e8e9e4f8b9dd795248b3efc8d Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 31 Jan 2017 12:57:09 -0700 Subject: [PATCH] Adjusted form --- routes/api/account/index.js | 2 +- services/users.js | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/routes/api/account/index.js b/routes/api/account/index.js index 44749a488..db11d391e 100644 --- a/routes/api/account/index.js +++ b/routes/api/account/index.js @@ -28,7 +28,7 @@ router.post('/email/confirm', (req, res, next) => { UsersService .verifyEmailConfirmation(token) - .then(([, referer]) => { + .then(({referer}) => { res.json({redirectUri: referer}); }) .catch((err) => { diff --git a/services/users.js b/services/users.js index d3751f0c8..e84e4f4d7 100644 --- a/services/users.js +++ b/services/users.js @@ -543,6 +543,9 @@ module.exports = class UsersService { return Promise.reject('email is required when creating a JWT for resetting passord'); } + // Conform the email to lowercase. + email = email.toLowerCase(); + const tokenOptions = { jwtid: uuid.v4(), algorithm: 'HS256', @@ -550,13 +553,12 @@ module.exports = class UsersService { subject: EMAIL_CONFIRM_JWT_SUBJECT }; - email = email.toLowerCase(); let userPromise; if (!userID) { - // if there is no userID, we're coming from the endpoint where a new user is re-requesting a confirmation email - // and we don't know the userID + // If there is no userID, we're coming from the endpoint where a new user + // is re-requesting a confirmation email and we don't know the userID. userPromise = UserModel.findOne({profiles: {$elemMatch: {id: email, provider: 'local'}}}); } else { userPromise = UsersService.findById(userID); @@ -564,7 +566,7 @@ module.exports = class UsersService { return userPromise.then((user) => { if (!user) { - return Promise.reject(new Error('user not found')); + return Promise.reject(errors.ErrNotFound); } // Get the profile representing the local account. @@ -575,13 +577,11 @@ module.exports = class UsersService { return Promise.reject(new Error('email address already confirmed')); } - const payload = { + return jwt.sign({ email, referer, userID: user.id - }; - - return jwt.sign(payload, process.env.TALK_SESSION_SECRET, tokenOptions); + }, process.env.TALK_SESSION_SECRET, tokenOptions); }); } @@ -598,8 +598,7 @@ module.exports = class UsersService { subject: EMAIL_CONFIRM_JWT_SUBJECT }) .then(({userID, email, referer}) => { - - const userUpdate = UserModel + return UserModel .update({ id: userID, profiles: { @@ -612,9 +611,8 @@ module.exports = class UsersService { $set: { 'profiles.$.metadata.confirmed_at': new Date() } - }); - - return Promise.all([userUpdate, referer]); + }) + .then(() => ({userID, email, referer})); }); }