-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-config.js
53 lines (47 loc) · 1.61 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const LocalStrategy = require('passport-local').Strategy
const { pool } = require('./dbConfig');
const bcrypt = require('bcrypt')
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
pool.query(
'SELECT * from public.accounts WHERE email = $1', [email], (err, results)=> {
if(err){
throw err;
}
console.log(results.row);
if (results.rows.length > 0){
const user = results.rows[0];
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err){
throw err
}
if(isMatch) {
return done(null, user);
} else {
return done(null, false, {message: 'Password is not correct'});
}
});
} else {
return done(null, false, {message: 'Email is not registered'});
}
}
);
};
passport.use(
new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
authenticateUser))
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => {
pool.query(
'SELECT * FROM public.accounts Where id=$1', [id], (err, results)=>{
if (err) {
throw err;
}
return done(null, results.rows[0]);
});
});
}
module.exports = initialize;