-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-config.js
35 lines (32 loc) · 1.33 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, getUserByusername, getUserById, req) {
const authenticateUser = async (username, password, done) => {
const user = await getUserByusername(username);
if(user === null || user === '') {
return done(null, false, { message: 'User does not exist'});
}
//done(<errorType>, <user:object, false>, <message>)
//https://www.youtube.com/watch?v=gzDB0ZGOjA0 > 12:59
try {
if(await bcrypt.compare(password, user.password)) {
if(!user.isVerified)
return done(null, false, { message: 'Account not activated. Sign up again to re-send token.' });
else
return done(null, user);
} else {
return done(null, false, { message: 'Username or Password incorrect' });
}
} catch (e) {
return done(e);
}
};
passport.use(new LocalStrategy({usernameField: 'username', passwordField: 'password'}, authenticateUser));
passport.serializeUser((user, done) => {
return done(null, user._id);
});
passport.deserializeUser(async (id, done) => {
return done(null, await getUserById(id));
});
}
module.exports = initialize;