Skip to content

Regenerate Email Verification Token on Email Request #4439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions spec/EmailVerificationToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ describe("Email Verification Token Expiration: ", () => {
var user = new Parse.User();
var sendEmailOptions;
var sendVerificationEmailCallCount = 0;
let userBeforeRequest;
var emailAdapter = {
sendVerificationEmail: options => {
sendEmailOptions = options;
Expand All @@ -509,6 +510,15 @@ describe("Email Verification Token Expiration: ", () => {
return user.signUp();
})
.then(() => {
const config = Config.get('test');
return config.database.find('_User', {username: 'resends_verification_token'}).then((results) => {
return results[0];
});
})
.then((newUser) => {
// store this user before we make our email request
userBeforeRequest = newUser;

expect(sendVerificationEmailCallCount).toBe(1);

return requestp.post({
Expand All @@ -523,13 +533,25 @@ describe("Email Verification Token Expiration: ", () => {
json: true,
resolveWithFullResponse: true,
simple: false // this promise is only rejected if the call itself failed
})
.then((response) => {
expect(response.statusCode).toBe(200);
expect(sendVerificationEmailCallCount).toBe(2);
expect(sendEmailOptions).toBeDefined();
done();
});
});
})
.then((response) => {
expect(response.statusCode).toBe(200);
expect(sendVerificationEmailCallCount).toBe(2);
expect(sendEmailOptions).toBeDefined();

// query for this user again
const config = Config.get('test');
return config.database.find('_User', {username: 'resends_verification_token'}).then((results) => {
return results[0];
});
})
.then((userAfterRequest) => {
// verify that our token & expiration has been changed for this new request
expect(typeof userAfterRequest).toBe('object');
expect(userBeforeRequest._email_verify_token).not.toEqual(userAfterRequest._email_verify_token);
expect(userBeforeRequest._email_verify_token_expires_at).not.toEqual(userAfterRequest.__email_verify_token_expires_at);
done();
})
.catch(error => {
jfail(error);
Expand Down
14 changes: 12 additions & 2 deletions src/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,23 @@ export class UserController extends AdaptableController {
});
}

/**
* Regenerates the given user's email verification token
*
* @param user
* @returns {*}
*/
regenerateEmailVerifyToken(user) {
this.setEmailVerifyToken(user);
return this.config.database.update('_User', { username: user.username }, user);
}

resendVerificationEmail(username) {
return this.getUserIfNeeded({username: username}).then((aUser) => {
if (!aUser || aUser.emailVerified) {
throw undefined;
}
this.setEmailVerifyToken(aUser);
return this.config.database.update('_User', {username}, aUser).then(() => {
return this.regenerateEmailVerifyToken(aUser).then(() => {
this.sendVerificationEmail(aUser);
});
});
Expand Down
9 changes: 7 additions & 2 deletions src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,19 @@ export class UsersRouter extends ClassesRouter {
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `No user found with email ${email}`);
}
const user = results[0];

// remove password field, messes with saving on postgres
delete user.password;

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

const userController = req.config.userController;
userController.sendVerificationEmail(user);
return { response: {} };
return userController.regenerateEmailVerifyToken(user).then(() => {
userController.sendVerificationEmail(user);
return { response: {} };
});
});
}

Expand Down