-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport.js
54 lines (47 loc) · 1.33 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
const passport= require('passport');
const LocalStrategy= require('passport-local').Strategy;
const User= require('./models/users').users;
passport.use(new LocalStrategy(
function(username, password, done) {
console.log('Inside LocalStrategy');
User.findOne({
username: username
}
).then((user)=>{
if (!user) {
console.log('user not found');
return done(null, false, {message: "No such user"});
}
if (user.password !== password) {
console.log('wrong password');
return done(null, false, {message: "Wrong password"});
}
console.log('success');
return done(null, user);
}).catch((err)=>{
console.log('error in login');
return done(err);
})
}
));
passport.serializeUser(function(user, done) {
console.log(user.username);
done(null, user.username);
});
passport.deserializeUser(function(username, done) {
console.log(username);
User.findOne({
username: username
}).then((user)=>{
if(!user){
return done(new Error('No such user'));
}
console.log("sending to login.js (from deserialize call)");
return done(null, user);
}).catch((err)=>{
return done(err);
})
});
module.exports= {
passport
};