-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport-config.js
41 lines (33 loc) · 1.13 KB
/
passport-config.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
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcryptjs');
const mongoose = require('mongoose');
const User = mongoose.model("User");
function initPassport(passport, getUserbyName) {
const authenticateUser = async (name, password, done) => {
const user = await getUserbyName(name);
if (user == null) {
return done(null, false, { message: "No user registered with this email" })
}
try {
const match = await bcrypt.compare(password, user.password);
if (match) {
return done(null, user);
}
else {
return done(null, false, { message: "Wrong username or password" });
}
} catch (error) {
return done(error);
}
}
passport.use(new LocalStrategy({ usernameField: 'name' }, authenticateUser))
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
}
module.exports = initPassport;