-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticate.js
31 lines (26 loc) · 1.01 KB
/
authenticate.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
const jwt = require("jsonwebtoken");
const LocalStrategy = require("passport-local").Strategy;
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const config = require("./config");
const passport = require("passport");
const User = require("./models/user");
exports.local = passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
exports.getToken = (user) =>
jwt.sign(user, config.secret, { expiresIn: "24h" });
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.secret,
};
exports.jwtPassport = passport.use(
new JwtStrategy(opts, (jwtPayload, done) => {
User.findOne({ _id: jwtPayload._id }, (err, user) => {
if (err) return done(err, false);
else if (user) return done(null, user);
else return done(null, false);
});
})
);
exports.verifyUser = passport.authenticate("jwt", { session: false });