-
Notifications
You must be signed in to change notification settings - Fork 3
/
passport.js
85 lines (74 loc) · 2.35 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const passport= require('passport');
const LocalStrategy= require('passport-local').Strategy;
const User= require('./database').users;
const account_status= require('./database').account_status;
const events= require('events');
const EventEmitter= new events.EventEmitter();
passport.use(new LocalStrategy(
function(username, password, done) {
console.log('Inside LocalStrategy');
User.findOne({
where: {
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) {
done(null, user.username);
});
passport.deserializeUser(function(username, done) {
User.findOne({
where: {
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);
//checking for account status
account_status.findOne({
where: {
username: username
}
}).then((account_user)=>{
if(!account_user){
console.log('user not found');
return done(null, false, {message: "No such user"});
}
if(account_user.status=="deactive"){
console.log("The account has been deactivated by the admin(deserialize call)");
//recently disabled//EventEmitter.emit("user_logout",{username: username}); //event call
return done(null,{message: "deactivated",});
} else {
console.log("The account is active");
//recently disabled//EventEmitter.emit("user_login",{username: username}); //event call
return done(null, user);
}
}).catch((err)=>{
return done(err);
})
}).catch((err)=>{
return done(err);
})
});
module.exports= {
passport,
EventEmitter
};