fix(user): check if email is someone else

This commit is contained in:
Mrugesh Mohapatra
2018-05-28 19:23:17 +05:30
parent f52d5b5369
commit 924cf5ae49

View File

@ -640,12 +640,31 @@ module.exports = function(User) {
emailVerified: false, emailVerified: false,
emailVerifyTTL: new Date() emailVerifyTTL: new Date()
}; };
return Observable.forkJoin(
this.update$(updateConfig), // defer prevents the promise from firing prematurely (before subscribe)
this.requestAuthEmail(false, newEmail), return Observable.defer(() => User.doesExist(null, newEmail))
(user, message) => ({ user, message }) .do(exists => {
) if (exists && !isOwnEmail) {
.map(({ message }) => message); // newEmail is not associated with this account,
// but is associated with different account
throw wrapHandledError(
new Error('email already in use'),
{
type: 'info',
message:
`${newEmail} is already associated with another account.`
}
);
}
})
.flatMap(()=>{
return Observable.forkJoin(
this.update$(updateConfig),
this.requestAuthEmail(false, newEmail),
(user, message) => ({ user, message })
)
.map(({ message }) => message);
});
} else { } else {
return 'Something unexpected happened whilst updating your email.'; return 'Something unexpected happened whilst updating your email.';