-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.js
37 lines (36 loc) · 1.23 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { lock } = require('../../account/utils');
const { sendEmail } = require('../../../comms');
const { invalidate } = require('./invalidate');
const { increment } = require('./increment');
module.exports = {
validate: async ({ hash, model, emailTemplate, givenUsername }) => {
let matchingUser = await model.findOne({ resetPasswordHash: hash });
if (matchingUser === null) return;
if (matchingUser.locked) return; // define ?
await increment(matchingUser, 'tryPasswordAttempts', model);
if (matchingUser.username === givenUsername) {
sendEmail(
matchingUser.email && matchingUser.email !== ''
? matchingUser.email
: matchingUser.username,
null,
emailTemplate,
null // firstName
);
return true;
} else if (matchingUser.tryPasswordAttempts >= 2) {
const locked = await lock('resetPasswordHash', hash, model);
if (locked.ok)
sendEmail(
matchingUser.email && matchingUser.email !== ''
? matchingUser.email
: matchingUser.username,
null,
'Locked',
null // firstName
);
await invalidate(hash, model);
}
return matchingUser.tryPasswordAttempts + 1;
},
};