-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
43 lines (39 loc) · 1.19 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
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const JwtStrategy = require('passport-jwt').Strategy;
const User = require('./models/User');
const cookieExtractor = req => {
let token = null;
if (req && req.cookies) {
token = req.cookies["access_token"];
}
return token;
}
// authorization
passport.use(new JwtStrategy({
jwtFromRequest: cookieExtractor,
//MUST BE CHANGED BEFORE PRODUCTION DEPLOYMENT
secretOrKey: "Maskify"
}, (payload, done) => {
User.findById({ _id: payload.sub }, (err, user) => {
if (err)
return done(err, false);
if (user)
return done(null, user);
else
return done(null, false);
});
}));
// authenticated local strategy using username and password
passport.use(new LocalStrategy((username, password, done) => {
User.findOne({ username }, (err, user) => {
// something went wrong with database
if (err)
return done(err);
// if no user exist
if (!user)
return done(null, false);
// check if password is correct
user.comparePassword(password, done);
});
}));