-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport_config.js
36 lines (31 loc) · 1007 Bytes
/
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
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");
function initialize(passport, getUserByUsername, getUserById) {
const authenticateUser = async (username, password, done) => {
const [user] = await getUserByUsername(username);
console.log("hola");
console.log(user);
if (user == null) {
return done(null, false, { message: "No user found.." });
}
try {
if (await bcrypt.compare(password, user.password)) {
console.log("passwd correct");
return done(null, user);
} else {
console.log("passwd invalid");
return done(null, false, { message: "error passwd invalid" });
}
} catch (e) {
return done(e, false);
}
};
passport.use(new LocalStrategy(authenticateUser));
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser(async (id, done) => {
done(null, await getUserById(id));
});
}
module.exports = initialize;