-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-config.js
39 lines (38 loc) · 1.41 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
const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')
// const sql = require('mssql');
function initialize(passport, getUserByEmail, getUserById){
const authenticateUser = async (email, password, done) => {
try{
email = email.replace(/'/g, '')
const result = await getUserByEmail(email)
if(result.recordset.length == 0){
return done(null, false, {message: 'No user with that email'})
}
const user = result.recordset[0]
// console.log(password)
// console.log(user.password)
try {
if(await bcrypt.compare(password, user.password)){
return done(null, user)
} else {
return done(null, false, {message: 'Password incorrect'})
}
} catch (e) {
return done(e)
}
}catch(err){
console.log(err)
return done(null, false, {message: err.message})
}
}
passport.use(new LocalStrategy({usernameField: 'email', session: true},
authenticateUser))
passport.serializeUser((user, done) => done(null, user.id))
passport.deserializeUser(async (id, done) => {
const result = await getUserById(id)
const user = result.recordset[0]
return done(null, user)
})
}
module.exports = initialize