-
Notifications
You must be signed in to change notification settings - Fork 0
/
passportConfig.js
63 lines (53 loc) · 1.43 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
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");
const { pool } = require("./dbconfig");
function initialize(passport) {
const authenticateUser = (email, password, done) => {
pool.query(
`SELECT * FROM "users" WHERE "email" = $1`,
[email],
(err, results) => {
if (err) {
console.log(err);
}
if (results.rows.length > 0) {
const user = results.rows[0];
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) {
console.log("passport :", err);
}
if (isMatch) {
return done(null, user);
} else {
return done(null, false, { message: "Password is incorrect" });
}
});
} else {
return done(null, false, {
message: "No user with that email address",
});
}
}
);
};
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) {
return done(err);
}
return done(null, results.rows[0]);
}
);
});
}
module.exports = initialize;