fix(Observables): Remove observables from requestUpdateEmail method

This commit is contained in:
Stuart Taylor
2018-05-26 12:54:54 +01:00
committed by Mrugesh Mohapatra
parent 12b2c556ec
commit 9cf1d67e02
4 changed files with 61 additions and 72 deletions

View File

@ -587,82 +587,66 @@ module.exports = function(User) {
User.prototype.requestUpdateEmail = function requestUpdateEmail(newEmail) { User.prototype.requestUpdateEmail = function requestUpdateEmail(newEmail) {
const currentEmail = this.email; const currentEmail = this.email;
return Observable.defer(() => { const isOwnEmail = isTheSame(newEmail, currentEmail);
const isOwnEmail = isTheSame(newEmail, currentEmail); const sameUpdate = isTheSame(newEmail, this.newEmail);
const sameUpdate = isTheSame(newEmail, this.newEmail); const messageOrNull = getWaitMessage(this.emailVerifyTTL);
const messageOrNull = getWaitMessage(this.emailVerifyTTL); if (isOwnEmail) {
if (isOwnEmail) { if (this.emailVerified) {
if (this.emailVerified) { // email is already associated and verified with this account
// email is already associated and verified with this account
throw wrapHandledError(
new Error('email is already verified'),
{
type: 'info',
message: `${newEmail} is already associated with this account.`
}
);
} else if (!this.emailVerified && messageOrNull) {
// email is associated but unverified and
// email is within time limit
throw wrapHandledError(
new Error(),
{
type: 'info',
message: messageOrNull
}
);
}
}
if (sameUpdate && messageOrNull) {
// trying to update with the same newEmail and
// confirmation email is still valid
throw wrapHandledError( throw wrapHandledError(
new Error(), new Error('email is already verified'),
{ {
type: 'info', type: 'info',
message: dedent` message: `${newEmail} is already associated with this account.`
We have already sent an email confirmation request to ${newEmail}.
Please check your inbox.`
} }
); );
} } else if (!this.emailVerified && messageOrNull) {
if (!isEmail('' + newEmail)) { // email is associated but unverified and
throw createEmailError(); // email is within time limit
}
// newEmail is not associated with this user, and
// this attempt to change email is the first or
// previous attempts have expired
return Observable.if(
() => isOwnEmail || (sameUpdate && messageOrNull),
Observable.empty(),
// defer prevents the promise from firing prematurely (before subscribe)
Observable.defer(() => User.doesExist(null, newEmail))
)
.do(exists => {
if (exists) {
// newEmail is not associated with this account,
// but is associated with different account
throw wrapHandledError( throw wrapHandledError(
new Error('email already in use'), new Error(),
{ {
type: 'info', type: 'info',
message: message: messageOrNull
`${newEmail} is already associated with another account.`
} }
); );
} }
}
if (sameUpdate && messageOrNull) {
// trying to update with the same newEmail and
// confirmation email is still valid
throw wrapHandledError(
new Error(),
{
type: 'info',
message: dedent`
We have already sent an email confirmation request to ${newEmail}.
Please check your inbox.`
}
);
}
if (!isEmail('' + newEmail)) {
throw createEmailError();
}
// newEmail is not associated with this user, and
// this attempt to change email is the first or
// previous attempts have expired
if (isOwnEmail || (sameUpdate && !messageOrNull)) {
const update = {
newEmail,
emailVerified: false,
emailVerifyTTL: new Date()
};
return this.update$(update).toPromise()
.then(() => {
Object.assign(this, update);
return;
}) })
.flatMap(() => { .then(() => this.requestAuthEmail(false, newEmail).toPromise());
const update = { } else {
newEmail, return 'Something unexpected happened whilst updating your email.';
emailVerified: false, }
emailVerifyTTL: new Date()
};
return this.update$(update)
.do(() => Object.assign(this, update))
.flatMap(() => this.requestAuthEmail(false, newEmail));
});
});
}; };
function requestCompletedChallenges() { function requestCompletedChallenges() {

View File

@ -38,6 +38,14 @@ module.exports = function enableAuthentication(app) {
ifUserRedirect, ifUserRedirect,
(req, res) => res.redirect(301, '/auth/auth0')); (req, res) => res.redirect(301, '/auth/auth0'));
router.get(
'/update-email',
ifNoUserRedirectHome,
(req, res) => res.render('account/update-email', {
title: 'Update your email'
})
);
router.get('/signout', (req, res) => { router.get('/signout', (req, res) => {
req.logout(); req.logout();
req.session.destroy( (err) => { req.session.destroy( (err) => {

View File

@ -37,13 +37,9 @@ export default function settingsController(app) {
.withMessage('Email format is invalid.') .withMessage('Email format is invalid.')
]; ];
function updateMyEmail(req, res, next) { function updateMyEmail(req, res) {
const { user, body: { email } } = req; const { user, body: { email } } = req;
return user.requestUpdateEmail(email) return res.json({ message: user.requestUpdateEmail(email) } );
.subscribe(
message => res.json({ message }),
next
);
} }
const updateMyCurrentChallengeValidators = [ const updateMyCurrentChallengeValidators = [

View File

@ -4,7 +4,8 @@ const ALLOWED_METHODS = ['GET'];
const EXCLUDED_PATHS = [ const EXCLUDED_PATHS = [
'/api/flyers/findOne', '/api/flyers/findOne',
'/signout', '/signout',
'/settings/update-email' '/update-email',
'/passwordless-change'
]; ];
export default function emailNotVerifiedNotice() { export default function emailNotVerifiedNotice() {
@ -23,7 +24,7 @@ export default function emailNotVerifiedNotice() {
confirm. confirm.
` `
); );
res.redirect('/settings/update-email'); res.redirect('/update-email');
return next; return next;
} }
} }