-
Notifications
You must be signed in to change notification settings - Fork 0
/
passportConfig.js
42 lines (35 loc) · 1.45 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
const LocalStrategy = require('passport-local').Strategy;
const { pool } = require('./dbConfig');
const bcrypt = require('bcrypt');
function initialize(passport) {
const authenticateUser = async (email, password, done) => {
try {
const results = await pool.query(`SELECT * FROM user_account WHERE email = $1`, [email]);
//console.log(results.rows);
if (results.rows.length > 0) {
const user = results.rows[0];
//console.log(`user reached here: ${user}`);
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) throw err;
if (isMatch) return done(null, user);
return done(null, false, {message: 'Password is NOT correct'});
});
} else {
return done(null, false, {message: 'Email is not registered'})
}
} catch (error) {
throw error;
}
}
passport.use(new LocalStrategy( { usernameField: 'email', passwordField: 'password' }, authenticateUser));
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser(async (id, done) => {
try {
const results = await pool.query(`SELECT * FROM user_account WHERE id = $1`, [id]);
return done(null, results.rows[0]);
} catch(error) {
throw error;
}
});
}
module.exports = initialize;