-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-config.js
32 lines (28 loc) · 974 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
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
//A function defined in passport-config to initialize passport
async function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
const user = await getUserByEmail(email);
if (user.length == 0) {
return done(null, false, { message: 'No user with that email' });
}
try {
if (await bcrypt.compare(password, user[0].password)) {
return done(null, user);
} else {
return done(null, false, { message: 'Password incorrect' });
}
} catch (e) {
return done(e);
}
};
await passport.use(
new LocalStrategy({ usernameField: 'email' }, authenticateUser)
);
passport.serializeUser((user, done) => done(null, user[0].user_id));
passport.deserializeUser((id, done) => {
return done(null, getUserById(id));
});
}
module.exports = initialize;