32 lines
762 B
JavaScript
32 lines
762 B
JavaScript
/**
|
|
* Config: Passport.js
|
|
*/
|
|
|
|
const StrategyJWT = require("passport-jwt").Strategy;
|
|
const ExtractJWT = require("passport-jwt").ExtractJwt;
|
|
|
|
const { users } = require("../models");
|
|
|
|
module.exports = (passport) => {
|
|
const options = {};
|
|
options.jwtFromRequest = ExtractJWT.fromAuthHeaderAsBearerToken();
|
|
options.secretOrKey = process.env.tokenSecret; // Change this to only use your own secret token
|
|
|
|
passport.use(
|
|
new StrategyJWT(options, async (jwtPayload, done) => {
|
|
let user;
|
|
try {
|
|
user = await users.getById(jwtPayload.id);
|
|
} catch (error) {
|
|
console.log(error);
|
|
return done(error, null);
|
|
}
|
|
|
|
if (!user) {
|
|
return done(null, false);
|
|
}
|
|
return done(null, user);
|
|
})
|
|
);
|
|
};
|