-
Notifications
You must be signed in to change notification settings - Fork 0
/
passportConfig.js
57 lines (48 loc) · 1.16 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
const localStrategy = require('passport-local').Strategy
const { pool } = require('./dbConfig')
const bcrypt = require('bcrypt')
function initialize(passport) {
const authenticateUser = (email, password, done) => {
pool.query('SELECT * FROM users WHERE email = $1', [email], (err, results) => {
if (err) {
throw err
}
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: 'Incorrect 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) => {
pool.query('SELECT * FROM users WHERE id = $1', [id], (err, results) => {
if (err) throw err
return done(null, results.rows[0])
})
})
}
module.exports = initialize