diff --git a/common/models/user.js b/common/models/user.js index 28483950b9..a34348f0ef 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -83,11 +83,6 @@ function getWaitPeriod(ttl) { return 0; } module.exports = function(User) { - // NOTE(berks): user email validation currently not needed but build in. This - // work around should let us sneak by - // see: - // https://github.com/strongloop/loopback/issues/1137#issuecomment-109200135 - delete User.validations.email; // set salt factor for passwords User.settings.saltWorkFactor = 5; // set user.rand to random number @@ -241,7 +236,7 @@ module.exports = function(User) { return User.findById(uid, (err, user) => { - if (err || !user) { + if (err || !user || !user.newEmail) { ctx.req.flash('error', { msg: dedent`Oops, something went wrong, please try again later` }); @@ -273,7 +268,16 @@ module.exports = function(User) { return ctx.res.redirect(redirect); } - return next(); + return user.update$({ + email: user.newEmail, + newEmail: null, + emailVerifyTTL: null + }) + .do(() => { + return next(); + }) + .toPromise(); + }); }); @@ -477,7 +481,7 @@ module.exports = function(User) { } ); - User.requestAuthLink = function requestAuthLink(email) { + User.requestAuthEmail = function requestAuthEmail(email) { if (!isEmail(email)) { return Promise.reject( new Error('The submitted email not valid.') @@ -550,7 +554,7 @@ module.exports = function(User) { }; User.remoteMethod( - 'requestAuthLink', + 'requestAuthEmail', { description: 'request a link on email with temporary token to sign in', accepts: [{ @@ -565,15 +569,17 @@ module.exports = function(User) { } ); - User.prototype.updateEmail = function updateEmail(email) { - const ownEmail = email === this.email; - if (!isEmail('' + email)) { + User.prototype.requestUpdateEmail = function requestUpdateEmail( + newEmail + ) { + const ownEmail = newEmail === this.email; + if (!isEmail('' + newEmail)) { return Observable.throw(createEmailError()); } // email is already associated and verified with this account if (ownEmail && this.emailVerified) { return Observable.throw(new Error( - `${email} is already associated with this account.` + `${newEmail} is already associated with this account.` )); } @@ -588,23 +594,25 @@ module.exports = function(User) { `); } - return Observable.fromPromise(User.doesExist(null, email)) + return Observable.fromPromise(User.doesExist(null, newEmail)) .flatMap(exists => { // not associated with this account, but is associated with another if (!ownEmail && exists) { return Promise.reject( - new Error(`${email} is already associated with another account.`) + new Error( + `${newEmail} is already associated with another account.` + ) ); } const emailVerified = false; return this.update$({ - email, + newEmail, emailVerified, emailVerifyTTL: new Date() }) .do(() => { - this.email = email; + this.newEmail = newEmail; this.emailVerified = emailVerified; this.emailVerifyTTL = new Date(); }); @@ -612,7 +620,7 @@ module.exports = function(User) { .flatMap(() => { const mailOptions = { type: 'email', - to: email, + to: newEmail, from: getEmailSender(), subject: 'freeCodeCamp - Email Update Requested', protocol: getProtocol(), @@ -625,7 +633,7 @@ module.exports = function(User) { 'server', 'views', 'emails', - 'user-email-verify.ejs' + 'user-request-update-email.ejs' ) }; return this.verify(mailOptions); diff --git a/common/models/user.json b/common/models/user.json index 521b36f9bf..c8152d7f96 100644 --- a/common/models/user.json +++ b/common/models/user.json @@ -16,6 +16,9 @@ } } }, + "newEmail":{ + "type": "string" + }, "emailVerifyTTL": { "type": "date" }, @@ -277,7 +280,7 @@ "principalType": "ROLE", "principalId": "$owner", "permission": "ALLOW", - "property": "updateEmail" + "property": "requestUpdateEmail" }, { "accessType": "EXECUTE", @@ -298,7 +301,7 @@ "principalType": "ROLE", "principalId": "$everyone", "permission": "ALLOW", - "property": "requestAuthLink" + "property": "requestAuthEmail" } ], "methods": {} diff --git a/server/boot/settings.js b/server/boot/settings.js index 5a407b080b..9264d16fcb 100644 --- a/server/boot/settings.js +++ b/server/boot/settings.js @@ -21,7 +21,7 @@ export default function settingsController(app) { function updateMyEmail(req, res, next) { const { user, body: { email } } = req; - return user.updateEmail(email) + return user.requestUpdateEmail(email) .subscribe( (message) => res.json({ message }), next diff --git a/server/boot/user.js b/server/boot/user.js index e167481e9d..ee7faee3eb 100644 --- a/server/boot/user.js +++ b/server/boot/user.js @@ -248,7 +248,7 @@ module.exports = function(app) { return res.redirect('/'); } - return User.requestAuthLink(req.body.email) + return User.requestAuthEmail(req.body.email) .then(msg => { return res.status(200).send({ message: msg }); }) diff --git a/server/views/emails/user-request-sign-in.ejs b/server/views/emails/user-request-sign-in.ejs index 866a77c96b..fc9cfb1bd6 100644 --- a/server/views/emails/user-request-sign-in.ejs +++ b/server/views/emails/user-request-sign-in.ejs @@ -14,4 +14,4 @@ Good luck with the challenges! Thanks, The freeCodeCamp Team. -team@freecodecamp.com +team@freecodecamp.org diff --git a/server/views/emails/user-request-sign-up.ejs b/server/views/emails/user-request-sign-up.ejs index fadeccf4bc..ec251e24e9 100644 --- a/server/views/emails/user-request-sign-up.ejs +++ b/server/views/emails/user-request-sign-up.ejs @@ -9,9 +9,9 @@ This above link is valid for 15 minutes. And when you have a moment: 1. Visit the settings page and link your account to GitHub. -2. Follow our Medium Publication: https://medium.freecodecamp.com -3. Checkout our forum: https://forum.freecodecamp.com -4. Join the conversation: https://gitter.im/FreeCodeCamp/FreeCodeCamp +2. Follow our Medium Publication: https://medium.freecodecamp.org +3. Checkout our forum: https://forum.freecodecamp.org +4. Join the conversation: https://gitter.im/freeCodeCamp/freeCodeCamp IMPORTANT NOTE: If you did not make any such request, simply delete or ignore this email. @@ -21,4 +21,4 @@ Good luck with the challenges! Thanks, The freeCodeCamp Team. -team@freecodecamp.com +team@freecodecamp.org diff --git a/server/views/emails/user-email-verify.ejs b/server/views/emails/user-request-update-email.ejs similarity index 93% rename from server/views/emails/user-email-verify.ejs rename to server/views/emails/user-request-update-email.ejs index db8df54f39..130f5f1263 100644 --- a/server/views/emails/user-email-verify.ejs +++ b/server/views/emails/user-request-update-email.ejs @@ -10,4 +10,4 @@ Good luck with the challenges! Thanks, The freeCodeCamp Team. -team@freecodecamp.com +team@freecodecamp.org