-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassport-config.js
More file actions
28 lines (23 loc) · 908 Bytes
/
passport-config.js
File metadata and controls
28 lines (23 loc) · 908 Bytes
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
import { Strategy } from 'passport-local';
import bcrypt from 'bcrypt';
function initialize(passport, getUserByEmail, getUserById){
const authenticateUser = async (email, password, done) => {
const user = getUserByEmail(email);
if(user == null) {
return done(null, false, {message: 'no user with that email!'});
}
try {
if (await bcrypt.compare(password, user.password)) {
return done(null, user)
} else {
return done(null, false, { message: 'Password incorrect' })
}
} catch {
return done(e);
}
}
passport.use(new Strategy({ usernameField: 'email' }, authenticateUser));
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => done(null, getUserById(id)));
};
export default initialize;