Advertisement
nodejsdeveloperskh

passportjs solid conf for jwt

Apr 15th, 2022 (edited)
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const JwtStrategy = require('passport-jwt').Strategy;
  2. const ExtractJwt = require('passport-jwt').ExtractJwt;
  3.  
  4. const jwtConfigurations = {
  5.     jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  6.     secretOrKey: process.env.JWT_SECRET,
  7.     issuer: 'accounts.examplesoft.com',
  8.     audience: 'yoursite.net'
  9. }
  10. passport.use(new JwtStrategy(jwtConfigurations, jwtStrategyVerifyCallback));
  11.  
  12. function jwtStrategyVerifyCallback (jwtPayload, done) {
  13.     User.findOne({id: jwtPayload.sub}, function(err, user) {
  14.         if (err) {
  15.             return done(err, false);
  16.         }
  17.         if (user) {
  18.             return done(null, user);
  19.         } else {
  20.             return done(null, false);
  21.             // or you could create a new account
  22.         }
  23.     });
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement