feat(User): Refactor wait period logic
This commit is contained in:
committed by
mrugesh mohapatra
parent
b6f621fee3
commit
60e2baf307
@ -81,13 +81,30 @@ function getWaitPeriod(ttl) {
|
|||||||
const lastEmailSentAt = moment(new Date(ttl || null));
|
const lastEmailSentAt = moment(new Date(ttl || null));
|
||||||
const isWaitPeriodOver = ttl ?
|
const isWaitPeriodOver = ttl ?
|
||||||
lastEmailSentAt.isBefore(fiveMinutesAgo) : true;
|
lastEmailSentAt.isBefore(fiveMinutesAgo) : true;
|
||||||
|
|
||||||
if (!isWaitPeriodOver) {
|
if (!isWaitPeriodOver) {
|
||||||
const minutesLeft = 5 -
|
const minutesLeft = 5 -
|
||||||
(moment().minutes() - lastEmailSentAt.minutes());
|
(moment().minutes() - lastEmailSentAt.minutes());
|
||||||
return minutesLeft;
|
return minutesLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getWaitMessage(ttl) {
|
||||||
|
const minutesLeft = getWaitPeriod(ttl);
|
||||||
|
if (minutesLeft <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const timeToWait = minutesLeft ?
|
||||||
|
`${minutesLeft} minute${minutesLeft > 1 ? 's' : ''}` :
|
||||||
|
'a few seconds';
|
||||||
|
|
||||||
|
return dedent`
|
||||||
|
Please wait ${timeToWait} to resend an authentication link.
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function(User) {
|
module.exports = function(User) {
|
||||||
// set salt factor for passwords
|
// set salt factor for passwords
|
||||||
User.settings.saltWorkFactor = 5;
|
User.settings.saltWorkFactor = 5;
|
||||||
@ -477,15 +494,15 @@ module.exports = function(User) {
|
|||||||
|
|
||||||
User.prototype.requestAuthEmail = function requestAuthEmail() {
|
User.prototype.requestAuthEmail = function requestAuthEmail() {
|
||||||
return Observable.defer(() => {
|
return Observable.defer(() => {
|
||||||
const minutesLeft = getWaitPeriod(this.emailAuthLinkTTL);
|
const messageOrNull = getWaitMessage(this.emailAuthLinkTTL);
|
||||||
if (minutesLeft > 0) {
|
if (messageOrNull) {
|
||||||
const timeToWait = minutesLeft ?
|
throw wrapHandledError(
|
||||||
`${minutesLeft} minute${minutesLeft > 1 ? 's' : ''}` :
|
new Error('request is throttled'),
|
||||||
'a few seconds';
|
{
|
||||||
|
type: 'info',
|
||||||
return Observable.of(dedent`
|
message: messageOrNull
|
||||||
Please wait ${timeToWait} to resend an authentication link.
|
}
|
||||||
`);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a temporary access token with ttl for 15 minutes
|
// create a temporary access token with ttl for 15 minutes
|
||||||
@ -541,18 +558,6 @@ module.exports = function(User) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const minutesLeft = getWaitPeriod(this.emailVerifyTTL);
|
|
||||||
if (ownEmail && minutesLeft > 0) {
|
|
||||||
const timeToWait = minutesLeft ?
|
|
||||||
`${minutesLeft} minute${minutesLeft > 1 ? 's' : ''}` :
|
|
||||||
'a few seconds';
|
|
||||||
|
|
||||||
debug('request before wait time : ' + timeToWait);
|
|
||||||
|
|
||||||
return Observable.of(dedent`
|
|
||||||
Please wait ${timeToWait} to resend an authentication link.
|
|
||||||
`);
|
|
||||||
}
|
|
||||||
// defer prevents the promise from firing prematurely
|
// defer prevents the promise from firing prematurely
|
||||||
return Observable.defer(() => User.doesExist(null, newEmail));
|
return Observable.defer(() => User.doesExist(null, newEmail));
|
||||||
})
|
})
|
||||||
@ -566,6 +571,18 @@ module.exports = function(User) {
|
|||||||
message: `${newEmail} is already associated with another account.`
|
message: `${newEmail} is already associated with another account.`
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
const messageOrNull = getWaitMessage(this.emailVerifyTTL);
|
||||||
|
// email is already associated but unverified
|
||||||
|
if (messageOrNull) {
|
||||||
|
// email is within time limit
|
||||||
|
throw wrapHandledError(
|
||||||
|
new Error(),
|
||||||
|
{
|
||||||
|
type: 'info',
|
||||||
|
message: messageOrNull
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const emailVerified = false;
|
const emailVerified = false;
|
||||||
|
Reference in New Issue
Block a user