-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
33 lines (32 loc) · 883 Bytes
/
passport.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
const db = require('./db.js')
const passport = require('passport')
const localStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')
passport.use(new localStrategy(async (username, password, done) => {
let foundUser = await db.query('SELECT * FROM users WHERE username = $1', [username])
if (foundUser.rows[0]) {
let decrypted = await bcrypt.compare(password, foundUser.rows[0].password)
if (decrypted) {
done(null, foundUser.rows[0])
}
else {
done(null, false)
}
}
else {
done(null, false)
}
}))
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser(async (userID, done) => {
try {
let foundUser = await db.query('SELECT * FROM users WHERE id = $1', [userID])
done(null, foundUser.rows[0])
}
catch (err) {
done(err, false)
}
})
module.exports = passport