Skip to content

Commit db8594d

Browse files
montymxbflovilmart
authored andcommitted
Regenerate Email Verification Token on Email Request (#4439)
* regenerate email verification token & expiration in /verificationEmailRequest * Remove password field when saving on postgres
1 parent f2e21b0 commit db8594d

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

spec/EmailVerificationToken.spec.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ describe("Email Verification Token Expiration: ", () => {
487487
var user = new Parse.User();
488488
var sendEmailOptions;
489489
var sendVerificationEmailCallCount = 0;
490+
let userBeforeRequest;
490491
var emailAdapter = {
491492
sendVerificationEmail: options => {
492493
sendEmailOptions = options;
@@ -509,6 +510,15 @@ describe("Email Verification Token Expiration: ", () => {
509510
return user.signUp();
510511
})
511512
.then(() => {
513+
const config = Config.get('test');
514+
return config.database.find('_User', {username: 'resends_verification_token'}).then((results) => {
515+
return results[0];
516+
});
517+
})
518+
.then((newUser) => {
519+
// store this user before we make our email request
520+
userBeforeRequest = newUser;
521+
512522
expect(sendVerificationEmailCallCount).toBe(1);
513523

514524
return requestp.post({
@@ -523,13 +533,25 @@ describe("Email Verification Token Expiration: ", () => {
523533
json: true,
524534
resolveWithFullResponse: true,
525535
simple: false // this promise is only rejected if the call itself failed
526-
})
527-
.then((response) => {
528-
expect(response.statusCode).toBe(200);
529-
expect(sendVerificationEmailCallCount).toBe(2);
530-
expect(sendEmailOptions).toBeDefined();
531-
done();
532-
});
536+
});
537+
})
538+
.then((response) => {
539+
expect(response.statusCode).toBe(200);
540+
expect(sendVerificationEmailCallCount).toBe(2);
541+
expect(sendEmailOptions).toBeDefined();
542+
543+
// query for this user again
544+
const config = Config.get('test');
545+
return config.database.find('_User', {username: 'resends_verification_token'}).then((results) => {
546+
return results[0];
547+
});
548+
})
549+
.then((userAfterRequest) => {
550+
// verify that our token & expiration has been changed for this new request
551+
expect(typeof userAfterRequest).toBe('object');
552+
expect(userBeforeRequest._email_verify_token).not.toEqual(userAfterRequest._email_verify_token);
553+
expect(userBeforeRequest._email_verify_token_expires_at).not.toEqual(userAfterRequest.__email_verify_token_expires_at);
554+
done();
533555
})
534556
.catch(error => {
535557
jfail(error);

src/Controllers/UserController.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,23 @@ export class UserController extends AdaptableController {
135135
});
136136
}
137137

138+
/**
139+
* Regenerates the given user's email verification token
140+
*
141+
* @param user
142+
* @returns {*}
143+
*/
144+
regenerateEmailVerifyToken(user) {
145+
this.setEmailVerifyToken(user);
146+
return this.config.database.update('_User', { username: user.username }, user);
147+
}
148+
138149
resendVerificationEmail(username) {
139150
return this.getUserIfNeeded({username: username}).then((aUser) => {
140151
if (!aUser || aUser.emailVerified) {
141152
throw undefined;
142153
}
143-
this.setEmailVerifyToken(aUser);
144-
return this.config.database.update('_User', {username}, aUser).then(() => {
154+
return this.regenerateEmailVerifyToken(aUser).then(() => {
145155
this.sendVerificationEmail(aUser);
146156
});
147157
});

src/Routers/UsersRouter.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,19 @@ export class UsersRouter extends ClassesRouter {
268268
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `No user found with email ${email}`);
269269
}
270270
const user = results[0];
271+
272+
// remove password field, messes with saving on postgres
273+
delete user.password;
271274

272275
if (user.emailVerified) {
273276
throw new Parse.Error(Parse.Error.OTHER_CAUSE, `Email ${email} is already verified.`);
274277
}
275278

276279
const userController = req.config.userController;
277-
userController.sendVerificationEmail(user);
278-
return { response: {} };
280+
return userController.regenerateEmailVerifyToken(user).then(() => {
281+
userController.sendVerificationEmail(user);
282+
return { response: {} };
283+
});
279284
});
280285
}
281286

0 commit comments

Comments
 (0)