question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Providing custom message when failed to extract token

See original GitHub issue

Are there any support for this ?

passport.use('jwt', new JwtStrategy({
        secretOrKey: authConfig.secret,
        jwtFromRequest: ExtractJwt.fromAuthHeader(), //If return null, finishes with empty message
        algorithms: authConfig.algorithms
    },
    function(jwt_payload, done) {
        //Never had the chance to call done with custom message
        Account.findById(jwt_payload.userId, function(err, account) {
            if (err) return done(err);
            if (!account) return done(null, false, 'invalid Token');
            return done(null, account, 'valid Token');
        });
    })
);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
azharuniversecommented, Jul 28, 2017

You can check for Error string in info to catch and provide custom messages.

passport.authenticate('jwt', (error, user, info) => {
    sails.log.info('isAuthenticated policy: ', error, user, info.name);

    if (info.name === 'TokenExpiredError') info.status = 401;
    if (info.name === 'JsonWebTokenError') info.status = 401;
    if (info.name === 'Error') info.status = 401;
    if (error || !user) return res.negotiate(error || info);

    req.user = user;
    next();

  })(req, res);
0reactions
mrtnzagustincommented, Apr 5, 2020

Manteiners should add this “info” param and examples to the http://www.passportjs.org/packages/passport-jwt/ docs. I could’t find the error until i read this issue. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices for API error handling and troubleshooting
The table below provides details about the main error cases that your application could meet. Missing credentials: e.g. missing Authorization ...
Read more >
Custom Error Messages with Mongoose - Stack Overflow
So I wanted to do something similar: var emailVerificationTokenSchema = mongoose. Schema({ email: {type: String, required: true, unique: [true, ...
Read more >
How to Customize and Validate the Json Message for ...
This use case describes how to customize and validates JSON message for GrantType client credential – AccessToken.
Read more >
Decode JWT runtime error troubleshooting | Apigee Edge
This error occurs if the JSON Web Token (JWT) specified in the <Source> element of the Decode JWT policy is malformed, invalid or...
Read more >
Access Token Response - OAuth 2.0 Simplified
invalid_grant – The authorization code (or user's password for the password grant type) is invalid or expired. This is also the error you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found