-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
46 lines (41 loc) · 1.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
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const Models = require('./models');
const passportJWT = require('passport-jwt');
let Users = Models.User;
let JWTStrategy = passportJWT.Strategy;
let ExtractJWT = passportJWT.ExtractJwt;
//HTTP authentication for login
passport.use( new LocalStrategy({
usernameField: 'Username',
passwordField: 'Password'
}, ( username, password, callback ) => {
console.log(`Username: ${username}, Password: ${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.'});
}
console.log('Login was successful');
return callback(null, user);
})
}));
//JWT authentication
passport.use( new JWTStrategy({
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: 'myFlix API secret key'
}, ( jwtPayload, callback ) => {
return Users.findById(jwtPayload._id)
.then((user) =>{
return callback(null, user);
})
.catch((error) => {
return callback(error);
});
}));