forked from darshanchaithram/edc-old-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
42 lines (40 loc) · 1.44 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
var localStrategy = require('passport-local').Strategy;
var User = require('./db/User');
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use(new localStrategy(function(username, password, done) {
User.findOne({ _id: username }, function(err, doc) {
var resultarray = [];
resultarray.push(doc);
if (err) {
return done(err);
} else {
if (doc) {
var valid = doc.comparePassword(password, doc.password);
var permission = false;
if (doc.acctype == "admin") {
permission = true;
}
if (valid) {
done(null, {
username: doc._id,
uname: doc.uname,
acctype: doc.acctype,
newuser: doc.newuser,
permission: permission,
});
} else {
return done(null, false, { message: 'Invalid Credentials' });
}
} else {
return done(null, false, { message: 'User doesnt exist' });
}
}
})
}))
}