-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
38 lines (32 loc) · 993 Bytes
/
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
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const User = require('./api/models/userModel');
const passportJWT = require("passport-jwt");
const JWTStrategy = passportJWT.Strategy;
const { ExtractJwt } = passportJWT;
passport.use(new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
},
function (username, password, cb) {
return User.findOne({ username, password })
.then(function (user) {
if (!user) {
return cb(null, false, { 'message': 'Incorrect email or password' });
}
return cb(null, user, { 'message': 'You\'re Logged in successfully' });
})
.catch(function (err) {
console.log('error from passport', err);
cb(err);
});
}
));
passport.use(new JWTStrategy({
jwtFromRequest: ExtractJwt.fromHeader("authorization"),
secretOrKey: "jwtsecretinstring"
},
function (jwtPayload, cb) {
return cb(null, jwtPayload);
}
));