Skip to content

Instantly share code, notes, and snippets.

@duartefdias
Created December 24, 2021 12:53
Show Gist options
  • Save duartefdias/a6bae90ecfe393609757f0b14d00a0ac to your computer and use it in GitHub Desktop.
Save duartefdias/a6bae90ecfe393609757f0b14d00a0ac to your computer and use it in GitHub Desktop.
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose');
import User from '../models/users';
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = process.env.JWT_SECRET;
module.exports = passport => {
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
User.findById(jwt_payload._id).then(user => {
if (user) return done(null, user);
return done(null, false);
}).catch(err => console.log(err));
})
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment