Skip to content

Commit a397c00

Browse files
authored
feat: upgrade to mongoose 7 (#370)
* chore: update mongoose * fix: wait for unlock save BREAKING CHANGE: Upgraded to mongoose 7 and removed node 12 support
1 parent 3ef9e74 commit a397c00

File tree

7 files changed

+417
-364
lines changed

7 files changed

+417
-364
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ module.exports = function (schema, options) {
295295
}
296296

297297
if (cb) {
298-
query.exec(cb);
298+
query
299+
.exec()
300+
.then((user) => cb(null, user))
301+
.catch(cb);
299302
return;
300303
}
301304

lib/authenticate.js

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,82 @@ module.exports = function (user, password, options, cb) {
1515
};
1616

1717
function authenticate(user, password, options, cb) {
18+
let promise = Promise.resolve();
19+
1820
if (options.limitAttempts) {
1921
const attemptsInterval = Math.pow(options.interval, Math.log(user.get(options.attemptsField) + 1));
2022
const calculatedInterval = attemptsInterval < options.maxInterval ? attemptsInterval : options.maxInterval;
2123

2224
if (Date.now() - user.get(options.lastLoginField) < calculatedInterval) {
2325
user.set(options.lastLoginField, Date.now());
24-
user.save(function (saveErr) {
25-
if (saveErr) {
26+
user
27+
.save()
28+
.then(function () {
29+
return cb(null, false, new errors.AttemptTooSoonError(options.errorMessages.AttemptTooSoonError));
30+
})
31+
.catch(function (saveErr) {
2632
return cb(saveErr);
27-
}
28-
return cb(null, false, new errors.AttemptTooSoonError(options.errorMessages.AttemptTooSoonError));
29-
});
33+
});
3034
return;
3135
}
3236

3337
if (user.get(options.attemptsField) >= options.maxAttempts) {
3438
if (options.unlockInterval && Date.now() - user.get(options.lastLoginField) > options.unlockInterval) {
3539
user.set(options.lastLoginField, Date.now());
3640
user.set(options.attemptsField, 0);
37-
user.save();
41+
42+
promise = user.save();
3843
} else {
3944
return cb(null, false, new errors.TooManyAttemptsError(options.errorMessages.TooManyAttemptsError));
4045
}
4146
}
4247
}
4348

44-
if (!user.get(options.saltField)) {
45-
return cb(null, false, new errors.NoSaltValueStoredError(options.errorMessages.NoSaltValueStoredError));
46-
}
47-
48-
pbkdf2(password, user.get(options.saltField), options, function (err, hashBuffer) {
49-
if (err) {
50-
return cb(err);
49+
promise.then(function () {
50+
if (!user.get(options.saltField)) {
51+
return cb(null, false, new errors.NoSaltValueStoredError(options.errorMessages.NoSaltValueStoredError));
5152
}
5253

53-
if (scmp(hashBuffer, Buffer.from(user.get(options.hashField), options.encoding))) {
54-
if (options.limitAttempts) {
55-
user.set(options.lastLoginField, Date.now());
56-
user.set(options.attemptsField, 0);
57-
user.save(function (saveErr, user) {
58-
if (saveErr) {
59-
return cb(saveErr);
60-
}
61-
return cb(null, user);
62-
});
63-
} else {
64-
return cb(null, user);
54+
pbkdf2(password, user.get(options.saltField), options, function (err, hashBuffer) {
55+
if (err) {
56+
return cb(err);
6557
}
66-
} else {
67-
if (options.limitAttempts) {
68-
user.set(options.lastLoginField, Date.now());
69-
user.set(options.attemptsField, user.get(options.attemptsField) + 1);
70-
user.save(function (saveErr) {
71-
if (saveErr) {
72-
return cb(saveErr);
73-
}
74-
if (user.get(options.attemptsField) >= options.maxAttempts) {
75-
return cb(null, false, new errors.TooManyAttemptsError(options.errorMessages.TooManyAttemptsError));
76-
} else {
77-
return cb(null, false, new errors.IncorrectPasswordError(options.errorMessages.IncorrectPasswordError));
78-
}
79-
});
58+
59+
if (scmp(hashBuffer, Buffer.from(user.get(options.hashField), options.encoding))) {
60+
if (options.limitAttempts) {
61+
user.set(options.lastLoginField, Date.now());
62+
user.set(options.attemptsField, 0);
63+
user
64+
.save()
65+
.then(function (user) {
66+
return cb(null, user);
67+
})
68+
.catch(function (saveErr) {
69+
return cb(saveErr);
70+
});
71+
} else {
72+
return cb(null, user);
73+
}
8074
} else {
81-
return cb(null, false, new errors.IncorrectPasswordError(options.errorMessages.IncorrectPasswordError));
75+
if (options.limitAttempts) {
76+
user.set(options.lastLoginField, Date.now());
77+
user.set(options.attemptsField, user.get(options.attemptsField) + 1);
78+
user
79+
.save()
80+
.then(function () {
81+
if (user.get(options.attemptsField) >= options.maxAttempts) {
82+
return cb(null, false, new errors.TooManyAttemptsError(options.errorMessages.TooManyAttemptsError));
83+
} else {
84+
return cb(null, false, new errors.IncorrectPasswordError(options.errorMessages.IncorrectPasswordError));
85+
}
86+
})
87+
.catch(function (saveErr) {
88+
return cb(saveErr);
89+
});
90+
} else {
91+
return cb(null, false, new errors.IncorrectPasswordError(options.errorMessages.IncorrectPasswordError));
92+
}
8293
}
83-
}
94+
});
8495
});
8596
}

0 commit comments

Comments
 (0)