-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
97 lines (92 loc) · 3.5 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
86
87
88
89
90
91
92
93
94
95
96
97
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy;
const passportJWT = require("passport-jwt");
const JWTStrategy = passportJWT.Strategy;
const ExtractJWT = passportJWT.ExtractJwt;
const UserModel = require('./Model/Users');
const bcrypt = require('bcrypt');
passport.use('user-local', new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function (email, password, cb) {
//this one is typically a DB call. Assume that the returned user object is pre-formatted and ready for storing in JWT
return UserModel.findByEmail(email)
.then(user => {
if (user.length === 0) {
return cb(null, false, { message: 'Incorrect email.' });
}
if (user[0].active === "no") {
return cb(null, false, { message: 'Account is lock' });
}
let ret = bcrypt.compareSync(password, user[0].password);
if (ret) {
return cb(null, user, { message: 'Logged In Successfully' });
}
return cb(null, false, { message: 'Incorrect password.' });
})
.catch(err => cb(err));
}
));
passport.use('user-facebook', new LocalStrategy({
usernameField: 'name',
passwordField: 'fbId'
},
function (name, fbId, cb) {
//this one is typically a DB call. Assume that the returned user object is pre-formatted and ready for storing in JWT
UserModel.findByFaceBookId(fbId).then((currentUser) => {
if (currentUser.length !== 0) {
return cb(null, currentUser, { message: 'Logged In Successfully' });
} else {
let newUser = {
name,
facebookId: fbId,
role: "student"
};
UserModel.add(newUser)
.then(res => {
return cb(null, [{ ...newUser, id: res.insertId }], { message: 'Logged In Successfully' });
});
}
})
}
));
passport.use('user-google', new LocalStrategy({
usernameField: 'name',
passwordField: 'googleId'
},
function (name, googleId, cb) {
//this one is typically a DB call. Assume that the returned user object is pre-formatted and ready for storing in JWT
UserModel.findByGoogleId(googleId).then((currentUser) => {
if (currentUser.length !== 0) {
return cb(null, currentUser, { message: 'Logged In Successfully' });
} else {
let newUser = {
name,
googleId,
role: "student"
};
UserModel.add(newUser)
.then(res => {
return cb(null, [{ ...newUser, id: res.insertId }], { message: 'Logged In Successfully' });
});
}
})
}
));
passport.use(new JWTStrategy({
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: 'your_jwt_secret'
},
function (jwtPayload, cb) {
//find the user in db if needed. This functionality may be omitted if you store everything you'll need in JWT payload.
return UserModel.singleById(jwtPayload.userId)
.then(user => {
//console.log('-----user', user);
return cb(null, user);
})
.catch(err => {
return cb(err);
});
}
));