Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing request parameter in JwtStrategy verify function documentation #35

Closed
mstueve opened this issue Oct 30, 2015 · 7 comments
Closed

Comments

@mstueve
Copy link

mstueve commented Oct 30, 2015

When first implementing the sample code I was getting an error that 'jwt_payload.sub' was undefined. Looking a little closer I discovered that the verify function takes 3 args - the request, the payload, and callback. So:

passport.use(new JwtStrategy(opts, function(jwt_payload, done) {

Should be

passport.use(new JwtStrategy(opts, function(req, jwt_payload, done) {

@roxeteer
Copy link

You need to opts.passReqToCallback to true to get the request as a parameter. Otherwise the callback will receive only payload and done.

@iamsebastian
Copy link

True. This can be closed.

@Shahidbis
Copy link

this is the code i am using and when i do console.log(jwt_payload.sub), it always says "undefined" and fetches the first record from DB. Can somebody please help?
var JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
var passport = require('passport');
var User = require('../models/user');
var config = require('../config/main');

// Setup work and export for the JWT passport strategy
module.exports = function(passport) {
var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
opts.secretOrKey = config.secret;

opts.passReqToCallback = true;
//opts.issuer = "accounts.examplesoft.com";
//opts.audience = "yoursite.net";
passport.use(new JwtStrategy(opts, function(req, jwt_payload, done) {
console.log(jwt_payload.sub);
// console.log(req);
User.findOne({id: jwt_payload.sub}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false);
// or you could create a new account
}
});
}));
};

@devendra1102
Copy link

The passport.use() will automatically run when you call the passport.authenticate('jwt', cb). You should pass the authorization in header with valid JWT signed(user) token. If it valid and non-expired token then payload.subwill have user otherwise it doesnt contain user.

@Shahidbis
Copy link

Fixed it.
Thanks for your help @devendra1102

@oyedotunsodiq045
Copy link

oyedotunsodiq045 commented Apr 29, 2018

show us d code at shahihedy

@GayathriMohan
Copy link

Is there any way by which I could also get the server response in the JWTStrategy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants