-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassport.js
64 lines (56 loc) · 1.65 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
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
//Import Passport
const passport = require("passport");
//Import LocalStrategy module
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require('bcryptjs')
//Import User Model
const Users = require("./models/sql/sequelize.js").Users;
//Serialize user
passport.serializeUser(function (user, done) {
done(null, user.id);
});
//De-Serialize User
passport.deserializeUser(function (id, done) {
Users.findById(id)
.then((user) => {
done(null, user);
})
.catch((err)=>{
console.log(err);
})
});
//Define LocalStrategy
const localstrategy = new LocalStrategy(
{
passReqToCallback : true
},
function (req, username, password, done) {
Users.findOne({
where: {
username: username
}
})
.then((user) => {
if (user == null) {
return done(null, false, req.flash("loginMsg","Username not found !"));
} else {
bcrypt.compare(password, user.password).then((res) => {
// res === true
if (res) {
return done(null, user);
}
else {
return done(null, false, req.flash("loginMsg","Password incorrect !"));
}
});
}
})
.catch((err)=>{
console.log(err);
})
}
);
//User "localstrategy" at "local"
passport.use('local', localstrategy);
//Expose passport to be used in server.js
module.exports = passport;