feature(email): add ability to verify current email
This commit is contained in:
@ -325,19 +325,42 @@ module.exports = function(User) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
User.prototype.updateEmail = function updateEmail(email) {
|
User.prototype.updateEmail = function updateEmail(email) {
|
||||||
|
const fiveMinutesAgo = moment().subtract(5, 'minutes');
|
||||||
|
const lastEmailSentAt = moment(new Date(this.emailVerifyTTL || null));
|
||||||
|
const ownEmail = email === this.email;
|
||||||
|
const isWaitPeriodOver = this.emailVerifyTTL ?
|
||||||
|
lastEmailSentAt.isBefore(fiveMinutesAgo) :
|
||||||
|
true;
|
||||||
|
|
||||||
if (!isEmail(email)) {
|
if (!isEmail(email)) {
|
||||||
return Promise.reject(
|
return Promise.reject(
|
||||||
new Error('The submitted email not valid')
|
new Error('The submitted email not valid.')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (this.email && this.email === email) {
|
// email is already associated and verified with this account
|
||||||
|
if (ownEmail && this.emailVerified) {
|
||||||
return Promise.reject(new Error(
|
return Promise.reject(new Error(
|
||||||
`${email} is already associated with this account.`
|
`${email} is already associated with this account.`
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ownEmail && !isWaitPeriodOver) {
|
||||||
|
const minutesLeft = 5 -
|
||||||
|
(moment().minutes() - lastEmailSentAt.minutes());
|
||||||
|
|
||||||
|
const timeToWait = minutesLeft ?
|
||||||
|
`${minutesLeft} minute${minutesLeft > 1 ? 's' : ''}` :
|
||||||
|
'a few seconds';
|
||||||
|
|
||||||
|
return Promise.reject(new Error(
|
||||||
|
`Please wait ${timeToWait} to resend email verification.`
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
return User.doesExist(null, email)
|
return User.doesExist(null, email)
|
||||||
.then(exists => {
|
.then(exists => {
|
||||||
if (exists) {
|
// not associated with this account, but is associated with another
|
||||||
|
if (!ownEmail && exists) {
|
||||||
return Promise.reject(
|
return Promise.reject(
|
||||||
new Error(`${email} is already associated with another account.`)
|
new Error(`${email} is already associated with another account.`)
|
||||||
);
|
);
|
||||||
@ -345,11 +368,14 @@ module.exports = function(User) {
|
|||||||
|
|
||||||
const emailVerified = false;
|
const emailVerified = false;
|
||||||
return this.update$({
|
return this.update$({
|
||||||
email, emailVerified
|
email,
|
||||||
|
emailVerified,
|
||||||
|
emailVerifyTTL: new Date()
|
||||||
})
|
})
|
||||||
.do(() => {
|
.do(() => {
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.emailVerified = emailVerified;
|
this.emailVerified = emailVerified;
|
||||||
|
this.emailVerifyTTL = new Date();
|
||||||
})
|
})
|
||||||
.flatMap(() => {
|
.flatMap(() => {
|
||||||
var mailOptions = {
|
var mailOptions = {
|
||||||
@ -379,7 +405,7 @@ module.exports = function(User) {
|
|||||||
.catch(error => {
|
.catch(error => {
|
||||||
debug(error);
|
debug(error);
|
||||||
return Observable.throw(
|
return Observable.throw(
|
||||||
'Oops, something went wrong, please try again later'
|
'Oops, something went wrong, please try again later.'
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.toPromise();
|
.toPromise();
|
||||||
|
@ -16,6 +16,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"emailVerifyTTL": {
|
||||||
|
"type": "date"
|
||||||
|
},
|
||||||
"password": {
|
"password": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
@ -15,8 +15,7 @@ block content
|
|||||||
.form-group
|
.form-group
|
||||||
input.input-lg.form-control(type='email', name='email', id='email', value=user.email || '', placeholder=user.email || 'Enter your new email', autofocus, required, autocomplete="off")
|
input.input-lg.form-control(type='email', name='email', id='email', value=user.email || '', placeholder=user.email || 'Enter your new email', autofocus, required, autocomplete="off")
|
||||||
.form-group
|
.form-group
|
||||||
button.btn.btn-lg.btn-primary.btn-block(type='submit')
|
button.btn.btn-lg.btn-primary.btn-block(type='submit')= !user.email || user.emailVerified ? 'Update my Email' : 'Verify Email'
|
||||||
| Update my Email
|
|
||||||
a.btn.btn-lg.btn-block.btn-primary.btn-link-social(href='/settings')
|
a.btn.btn-lg.btn-block.btn-primary.btn-link-social(href='/settings')
|
||||||
| Go back to Settings
|
| Go back to Settings
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user