-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
45 lines (34 loc) · 1.17 KB
/
passport.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
38
39
40
41
42
43
44
45
const LocalStrategy = require('passport-local').Strategy,
passport = require('passport'),
Admin = require('./database/models/Admin');
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
(email, password, done) => {
// Find the mail into the database
Admin.findOne({ email }, async (err, user) => {
if (err) { return done(err); }
// Check if the admin's email and the email insert are not the equal
if (!user) {
return done(null, false, { message: 'Incorrect email.' });
}
// Comparing the password with the admin's password
const isValid = await user.validatePassword(password);
// Check if they don't match
if (!isValid) {
return done(null, false, { message: 'Incorrect password.' });
}
// All checks passed
return done(null, user)
});
}
));
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
Admin.findById(id, (err, user) => {
done(err, user);
});
});