-
Notifications
You must be signed in to change notification settings - Fork 0
/
passportConfig.js
112 lines (79 loc) · 2.88 KB
/
passportConfig.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const LocalStrategy = require('passport-local').Strategy;
const db = require('./config/database');
const passport = require('passport');
const User = require('./models/User');
const { Op } = require('sequelize');
const bcrypt = require('bcrypt');
function initialize(passport){
const authenticateUser = (email, password, done) =>{
User.findAll({where:{email:{[Op.eq]: email}}})
.then(users => {
console.log(users)
if(users.length > 0){
const user = users[0];
bcrypt.compare(password, user.password, (err, isMatch) =>{
if(err){
throw err;
}
if(isMatch){
return done(null, user)
}else{
return done(null, false, {message: 'Wrong password'})
}
})
}else{
return done(null, false, {message: 'Email is not registered!'})
}
}).catch(err => {throw err})
// WiTH pg-library
// pool.query(
// `SELECT * FROM users WHERE email = $1`, [email],
// (err, results) =>{
// if(err){
// throw err;
// }
// console.log(results.rows);
// 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: 'Wrong password'})
// }
// })
// }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) => {
User.findAll({where:{id:{[Op.eq]: id}}})
.then(users => {
return done(null, users[0]);
})
.catch(err => {throw err})
// pool.query(
// `SELECT * FROM users WHERE id = $1`, [id],(err, results) =>{
// if(err){
// throw err;
// }
// else{
// return done(null, results.rows[0])
// }
// }
// )
})
}
module.exports = initialize;