-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
53 lines (48 loc) · 1.33 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
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const JwtStrategy = require("passport-jwt").Strategy;
const User = require("./models/User");
const keys = require("./config/keys");
// COOKIE EXTRACTOR FUNCTION
const cookieExtractor = (req) => {
let token = null;
if (req && req.cookies) {
token = req.cookies["access_token"];
}
return token;
};
passport.serializeUser(function (user, cb) {
cb(null, user);
});
passport.deserializeUser(function (obj, cb) {
cb(null, obj);
});
// used for authorization
passport.use(
new JwtStrategy(
{
jwtFromRequest: cookieExtractor,
secretOrKey: keys.JWT_SECRET_KEY,
},
(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);
});
}
)
);
// middleware for authentication using password and username, first time when user logs in.
passport.use(
new LocalStrategy((username, password, done) => {
User.findOne({ username }, (err, user) => {
//if something bad happens to db
if (err) return done(err);
//if user not on db
if (!user) return done(null, false);
// check for password
user.comparePassword(password, done);
});
})
);