Skip to content

Commit 59eeb99

Browse files
committed
Allow resetPassword if email is verified
1 parent e10dcf7 commit 59eeb99

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

common/models/user.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,14 @@ module.exports = function(User) {
566566
}
567567
// create a short lived access token for temp login to change password
568568
// TODO(ritch) - eventually this should only allow password change
569-
user.accessTokens.create({ttl: ttl}, function(err, accessToken) {
569+
if (UserModel.settings.emailVerificationRequired && !user.emailVerified) {
570+
err = new Error(g.f('Email has not been verified'));
571+
err.statusCode = 401;
572+
err.code = 'RESET_FAILED_EMAIL_NOT_VERIFIED';
573+
return cb(err);
574+
}
575+
576+
user.accessTokens.create({ ttl: ttl }, function(err, accessToken) {
570577
if (err) {
571578
return cb(err);
572579
}

test/user.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,43 @@ describe('User', function() {
21212121
});
21222122
});
21232123

2124+
describe('password reset with/without email verification', function() {
2125+
it('allows resetPassword by email if email verification is required and done',
2126+
function(done) {
2127+
User.settings.emailVerificationRequired = true;
2128+
var email = validCredentialsEmailVerified.email;
2129+
2130+
User.resetPassword({ email: email }, function(err, info) {
2131+
if (err) return done(err);
2132+
done();
2133+
});
2134+
});
2135+
2136+
it('disallows resetPassword by email if email verification is required and not done',
2137+
function(done) {
2138+
User.settings.emailVerificationRequired = true;
2139+
var email = validCredentialsEmail;
2140+
2141+
User.resetPassword({ email: email }, function(err) {
2142+
assert(err);
2143+
assert.equal(err.code, 'RESET_FAILED_EMAIL_NOT_VERIFIED');
2144+
assert.equal(err.statusCode, 401);
2145+
done();
2146+
});
2147+
});
2148+
2149+
it('allows resetPassword by email if email verification is not required',
2150+
function(done) {
2151+
User.settings.emailVerificationRequired = false;
2152+
var email = validCredentialsEmail;
2153+
2154+
User.resetPassword({ email: email }, function(err) {
2155+
if (err) return done(err);
2156+
done();
2157+
});
2158+
});
2159+
});
2160+
21242161
describe('ctor', function() {
21252162
it('exports default Email model', function() {
21262163
expect(User.email, 'User.email').to.be.a('function');

0 commit comments

Comments
 (0)