-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassportConfig.mysql.js
73 lines (65 loc) · 1.88 KB
/
passportConfig.mysql.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
64
65
66
67
68
69
70
71
72
73
const LocalStrategy = require("passport-local").Strategy;
const pool = require("./dbConfig.mysql");
const bcrypt = require("bcrypt");
function initialize(passport) {
function authenticateUser(email, pwd, done) {
//const pool = require("./dbConfig.mysql");
var sql = `SELECT * FROM customer WHERE email = "${email}"`;
console.log(`authenticateUser email: `, email);
console.log(`sql: `, sql);
console.log(`pool: `, pool);
pool.execute(sql, function(err, results) {
if (err) {
return done(err);
};
if (results.length > 0) {
console.log(`results[0].email: `, results[0].email);
const email = results[0];
if(email.pwd) {
email.pwd = email.pwd.trimEnd();
} else {
return done(null, false, {
message: "Missing password in database",
});
}
bcrypt.compare(pwd, email.pwd, function (err, isMatch) {
if (err) {
return done(err);
}
if (isMatch) {
return done(null, email);
} else {
return done(null, false, {
message: "Incorrect email or password",
});
}
});
} else {
return done(null, false, { message: "Missing email or password" });
}
});
};
passport.use(
new LocalStrategy(
{
usernameField: "email",
passwordField: "pwd",
},
authenticateUser
)
);
passport.serializeUser((email, done) => {
//console.error(`serializeUser: `, email.custId);
done(null, email.custid);
});
passport.deserializeUser((id, done) => {
pool.query(`SELECT * from customer WHERE custid = "${id}"`, function(err, results) {
if (err) {
throw err;
}
//console.error(`deserializeUser: `, results);
return done(null, results);
});
});
}
module.exports = initialize;