function (lockable, next) { //check if unlock token expired const isTokenExpired = !Utils.isAfter(new Date(), lockable.unlockTokenExpiryAt); if (isTokenExpired) { //if expired next(new Error('Unlock token expired')); } else { //otherwise continue with token verification next(null, lockable); } },
//TODO check presence of confirmation token function (next) { //check if confirmation token expired const isTokenExpiry = !Utils.isAfter(new Date(), confirmable.confirmationTokenExpiryAt); if (isTokenExpiry) { //if token expired next(new Error('Confirmation token expired')); } else { //otherwise continue with token verification next(null, confirmable); } },
schema.methods.isConfirmed = function (done) { //this context is of model instance const confirmable = this; //check if already confirmed const isConfirmed = (confirmable.confirmedAt && confirmable.confirmedAt !== null); //check if confirmation token expired const isTokenExpired = !Utils.isAfter(new Date(), confirmable.confirmationTokenExpiryAt); //account has not been confirmed //and token has not yet expire if (!isConfirmed && !isTokenExpired) { done(new Error('Account not confirmed')); } //account has not been confirmed and //confirmation token is expired else if (!isConfirmed && isTokenExpired) { //compose confirmation async .waterfall( [ function generateConfirmationToken(next) { confirmable.generateConfirmationToken(next); }, function sendConfirmation(confirmable, next) { confirmable.sendConfirmation(next); } ], function (error /*, confirmable*/ ) { //is there any error during //compose new confirmation? if (error) { done(error); } //new confirmation token is // and send successfully else { done(new Error( 'Confirmation token expired. Check your email for confirmation instructions.' )); } }); } //is confirmed else { done(null, confirmable); } };
schema.methods.isLocked = function (done) { //this context is of model instance const lockable = this; //check if already locked const isLocked = lockable.lockedAt && lockable.lockedAt !== null; //check if unlock token expired const isUnlockTokenExpired = !Utils.isAfter(new Date(), lockable.unlockTokenExpiryAt); //account is not locked //back-off if (!isLocked) { done(null, lockable); } //is locked and //unlock token is not expired else if (isLocked && !isUnlockTokenExpired) { done(new Error( 'Account locked. Check unlock instructions sent to you.')); } //is locked and //unlock token is expired else { //compose lock async.waterfall([ function generateUnlockToken(next) { lockable.generateUnlockToken(next); }, function sendUnLock(lockable, next) { lockable.sendUnLock(next); } ], function (error /*, lockable*/ ) { //is there any error during //compose new lock? if (error) { done(error); } //new unlock token generated // and send successfully else { done(new Error( 'Account locked. Check unlock instructions sent to you.' )); } }); } };
Confirmable.prototype.checkConfirmation = function(confirmable, done) { //this context is of Confirmable var self = this; //check if already confirmed var isConfirmed = (confirmable.confirmedAt && confirmable.confirmedAt !== null); //check if confirmation token expired var isTokenExpired = (!Utils.isAfter(new Date(), confirmable.confirmationTokenExpiryAt)); //is already confirmed if (!isConfirmed && !isTokenExpired) { done(new Error('Account not confirmed')); } //is not confirmed and //confirmation token is expired else if (!isConfirmed && isTokenExpired) { //compose confirmation self .composeConfirmation(confirmable, function(error, confirmable) { //is there any error during //compose new confirmation? if (error) { done(error); } //new confirmation token is // and send successfully else { done(new Error('Confirmation token expired. Check your email for confirmation.')); } }); } //is confirmed else { done(null, confirmable); } };