Created
December 24, 2021 12:53
-
-
Save duartefdias/a6bae90ecfe393609757f0b14d00a0ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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