-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
31 lines (27 loc) · 1.21 KB
/
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
const con = require("./mysql-connector.js");
const LocalStrategy = require('passport-local').Strategy;
module.exports = (passport) => {
passport.use(new LocalStrategy({ usernameField: 'username', passwordField: 'password' },
function (username, password, done) {
con.query(`SELECT * FROM user where (name ="${username}" and password="${password}") or (email ="${username}" and password="${password}")`, (err, result, fields) => {
if (err) {
console.log(err);
return done(err);
}
if (result.length == 0) {
console.log("failed attempt by user", username);
return done(null, false, { message: "incorrect password" });
}
else {
console.log("user ", result[0].name, "loggedin ");
return done(null, result);
}
});
}));
passport.serializeUser((user, done) => { done(null, user[0].name); });
passport.deserializeUser((id, done) => {
con.query(`SELECT * FROM user where name ="${id}"`, (err, result, fields) => {
done(null, result);
});
})
};