fix: resolve the query back to the promise

This commit is contained in:
Mrugesh Mohapatra
2018-05-28 18:47:36 +05:30
parent d3567fbb9b
commit f52d5b5369
2 changed files with 37 additions and 35 deletions

View File

@ -591,33 +591,26 @@ module.exports = function(User) {
User.prototype.requestAuthEmail = requestAuthEmail; User.prototype.requestAuthEmail = requestAuthEmail;
User.prototype.requestUpdateEmail = function requestUpdateEmail(newEmail) { User.prototype.requestUpdateEmail = function requestUpdateEmail(newEmail) {
const currentEmail = this.email; const currentEmail = this.email;
const isOwnEmail = isTheSame(newEmail, currentEmail); const isOwnEmail = isTheSame(newEmail, currentEmail);
const sameUpdate = isTheSame(newEmail, this.newEmail); const isResendUpdateToSameEmail = isTheSame(newEmail, this.newEmail);
const messageOrNull = getWaitMessage(this.emailVerifyTTL); const isLinkSentWithinLimit = getWaitMessage(this.emailVerifyTTL);
if (isOwnEmail) { const isVerifiedEmail = this.emailVerified;
if (this.emailVerified) {
// email is already associated and verified with this account if (isOwnEmail && isVerifiedEmail) {
throw wrapHandledError( // email is already associated and verified with this account
new Error('email is already verified'), throw wrapHandledError(
{ new Error('email is already verified'),
type: 'info', {
message: `${newEmail} is already associated with this account.` type: 'info',
} message: `
); ${newEmail} is already associated with this account.
} else if (!this.emailVerified && messageOrNull) { You can update a new email address instead.`
// email is associated but unverified and
// email is within time limit
throw wrapHandledError(
new Error(),
{
type: 'info',
message: messageOrNull
}
);
} }
);
} }
if (sameUpdate && messageOrNull) { if (isResendUpdateToSameEmail && isLinkSentWithinLimit) {
// trying to update with the same newEmail and // trying to update with the same newEmail and
// confirmation email is still valid // confirmation email is still valid
throw wrapHandledError( throw wrapHandledError(
@ -626,29 +619,34 @@ module.exports = function(User) {
type: 'info', type: 'info',
message: dedent` message: dedent`
We have already sent an email confirmation request to ${newEmail}. We have already sent an email confirmation request to ${newEmail}.
Please check your inbox.` ${isLinkSentWithinLimit}`
} }
); );
} }
if (!isEmail('' + newEmail)) { if (!isEmail('' + newEmail)) {
throw createEmailError(); throw createEmailError();
} }
// newEmail is not associated with this user, and // newEmail is not associated with this user, and
// this attempt to change email is the first or // this attempt to change email is the first or
// previous attempts have expired // previous attempts have expired
if (
if (isOwnEmail || (sameUpdate && !messageOrNull)) { !isOwnEmail ||
const update = { (isOwnEmail && !isVerifiedEmail) ||
(isResendUpdateToSameEmail && !isLinkSentWithinLimit)
) {
const updateConfig = {
newEmail, newEmail,
emailVerified: false, emailVerified: false,
emailVerifyTTL: new Date() emailVerifyTTL: new Date()
}; };
return this.update$(update).toPromise() return Observable.forkJoin(
.then(() => { this.update$(updateConfig),
Object.assign(this, update); this.requestAuthEmail(false, newEmail),
return; (user, message) => ({ user, message })
}) )
.then(() => this.requestAuthEmail(false, newEmail).toPromise()); .map(({ message }) => message);
} else { } else {
return 'Something unexpected happened whilst updating your email.'; return 'Something unexpected happened whilst updating your email.';
} }

View File

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