fix: Email should be switched before confirm

This commit is contained in:
Mrugesh Mohapatra
2017-10-28 00:24:00 +05:30
parent 5e61fd53d1
commit e652ad934a
2 changed files with 21 additions and 12 deletions

View File

@ -236,7 +236,7 @@ module.exports = function(User) {
return User.findById(uid, (err, user) => { return User.findById(uid, (err, user) => {
if (err || !user) { if (err || !user || !user.newEmail) {
ctx.req.flash('error', { ctx.req.flash('error', {
msg: dedent`Oops, something went wrong, please try again later` msg: dedent`Oops, something went wrong, please try again later`
}); });
@ -268,7 +268,16 @@ module.exports = function(User) {
return ctx.res.redirect(redirect); return ctx.res.redirect(redirect);
} }
return user.update$({
email: user.newEmail,
newEmail: null,
emailVerifyTTL: null
})
.do(() => {
return next(); return next();
})
.toPromise();
}); });
}); });
@ -561,16 +570,16 @@ module.exports = function(User) {
); );
User.prototype.requestUpdateEmail = function requestUpdateEmail( User.prototype.requestUpdateEmail = function requestUpdateEmail(
emailUpdateNew newEmail
) { ) {
const ownEmail = emailUpdateNew === this.email; const ownEmail = newEmail === this.email;
if (!isEmail('' + emailUpdateNew)) { if (!isEmail('' + newEmail)) {
return Observable.throw(createEmailError()); return Observable.throw(createEmailError());
} }
// email is already associated and verified with this account // email is already associated and verified with this account
if (ownEmail && this.emailVerified) { if (ownEmail && this.emailVerified) {
return Observable.throw(new Error( return Observable.throw(new Error(
`${emailUpdateNew} is already associated with this account.` `${newEmail} is already associated with this account.`
)); ));
} }
@ -585,25 +594,25 @@ module.exports = function(User) {
`); `);
} }
return Observable.fromPromise(User.doesExist(null, emailUpdateNew)) return Observable.fromPromise(User.doesExist(null, newEmail))
.flatMap(exists => { .flatMap(exists => {
// not associated with this account, but is associated with another // not associated with this account, but is associated with another
if (!ownEmail && exists) { if (!ownEmail && exists) {
return Promise.reject( return Promise.reject(
new Error( new Error(
`${emailUpdateNew} is already associated with another account.` `${newEmail} is already associated with another account.`
) )
); );
} }
const emailVerified = false; const emailVerified = false;
return this.update$({ return this.update$({
emailUpdateNew, newEmail,
emailVerified, emailVerified,
emailVerifyTTL: new Date() emailVerifyTTL: new Date()
}) })
.do(() => { .do(() => {
this.emailUpdateNew = emailUpdateNew; this.newEmail = newEmail;
this.emailVerified = emailVerified; this.emailVerified = emailVerified;
this.emailVerifyTTL = new Date(); this.emailVerifyTTL = new Date();
}); });
@ -611,7 +620,7 @@ module.exports = function(User) {
.flatMap(() => { .flatMap(() => {
const mailOptions = { const mailOptions = {
type: 'email', type: 'email',
to: emailUpdateNew, to: newEmail,
from: getEmailSender(), from: getEmailSender(),
subject: 'freeCodeCamp - Email Update Requested', subject: 'freeCodeCamp - Email Update Requested',
protocol: getProtocol(), protocol: getProtocol(),

View File

@ -16,7 +16,7 @@
} }
} }
}, },
"emailUpdateNew":{ "newEmail":{
"type": "string" "type": "string"
}, },
"emailVerifyTTL": { "emailVerifyTTL": {