-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
48 lines (45 loc) · 1.42 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
const passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
Models = require('./model.js'),
passportJWT = require('passport-jwt');
var Users = Models.User;
var JWTStrategy = passportJWT.Strategy;
var ExtractJWT = passportJWT.ExtractJwt;
passport.use(new LocalStrategy({
usernameField: 'Username',
passwordField: 'Password'
}, (username, password, callback) => {
console.log(username + ' ' + password);
Users.findOne({
Username: username
}, (error, user) => {
if (error) {
console.log(error);
return callback(error);
}
if (!user) {
console.log('incorrect username');
return callback(null, false, {
message: 'Incorrect username or password.'
});
}
if (!user.validatePassword(password)) {
console.log('incorrect password');
return callback(null, false, {message: 'Incorrect password.'});
}
console.log('finished');
return callback(null, user);
});
}));
passport.use(new JWTStrategy({
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: 'your_jwt_secret'
}, (jwtPayload, callback) => {
return Users.findById(jwtPayload._id)
.then((user) => {
return callback(null, user);
})
.catch((error) => {
return callback(error)
});
}));