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.

error always null when using custom callback

See original GitHub issue

The code I have, (the signature is from http://passportjs.org/docs/authenticate)

passport.authenticate('jwt', { session: false }, (err, user, info) => {
    if (err) {
      return next(err); 
    }
    if (!user) {
      return next(AuthenticationError(401, info));
    }
    req.user = user;
    return next();
  })(req, res, next);

However, in the case of malformed jwt or expired jwt, the err object is always null while the info object is carrying the error information. Is this designed on purpose or should be a bug?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
LukeAskewcommented, Dec 15, 2016

This was pretty confusing for me. If an expired or invalid token is not an exception, what is?

Here’s the middleware I ended up with.

function authenticate() {
  return (req, res, next) => {
    passport.authenticate('jwt', (err, data, info) => {
      if (err || info) {
        next(new Error('Unauthorized.'));
      } else {
        next();
      }
    })(req, res, next);
  };
}
3reactions
mikenicholsoncommented, Jan 11, 2017

@LukeAskew I wrote this module using passport-local as a model.

In passport-local, which was written by the author of the passport framework, failure to authenticate is not treated as an exception. Instead, invalid credentials results in the strategy calling self.fail() (which populates info). I treat expired or invalid JWT’s the same way the passport-local strategy would treat an incorrect password or a non-existent user.

Based on my understanding of passport-local, only things like database errors, etc. result in exceptions. To me this makes sense since exceptions should be reserved for unexpected errors. Invalid auth credentials are an expected case for an authentication framework and not treated as an exception by this module or passport-local.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Callback Listener always returning null - java - Stack Overflow
I am trying to create a simple SMS library for Android for which i have created some custom listener. I want ...
Read more >
ASPxGridView - The GetRowValues method returns null ...
I have an ASPxGridview which is bound at runtime on Page_Load. I am trying to add a custom callback button to create a...
Read more >
The function always returns null : r/Kotlin - Reddit
I'm calling the functions doIfFailureWithResult and doIfSuccessWithResult , inside the async block. I had to manually return a boolean ...
Read more >
AWS Lambda function handler in Node.js
The callback function takes two arguments: an Error and a response. When you call it, Lambda waits for the event loop to be...
Read more >
Advanced Callbacks | Dash for Python Documentation | Plotly
Catching Errors with PreventUpdate ... In certain situations, you don't want to update the callback output. You can achieve this by raising a...
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