-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpassport-config.js
35 lines (31 loc) · 1.04 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
const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
try {
const user = await getUserByEmail(email); // Await the promise from getUserByEmail
if (!user) {
return done(null, false, { message: 'No user with that email' });
}
const match = await bcrypt.compare(password, user.password);
if (match) {
return done(null, user);
} else {
return done(null, false, { message: 'Password incorrect' });
}
} catch (e) {
return done(e);
}
}
passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser));
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser(async (id, done) => {
try {
const user = await getUserById(id); // Await the promise from getUserById
done(null, user);
} catch (e) {
done(e, null);
}
});
}
module.exports = initialize;