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

Catch expired token throwing error #198

Open
george-norris-salesforce opened this issue Jul 11, 2018 · 3 comments
Open

Catch expired token throwing error #198

george-norris-salesforce opened this issue Jul 11, 2018 · 3 comments

Comments

@george-norris-salesforce

Running into the issue where expired token (using credentialsRequired false) is throwing error, and I want to redirect instead. What is the current correct way to handle this?

@mhaagens
Copy link

Same here.

@hobofan
Copy link

hobofan commented Sep 20, 2018

To catch errors in a specific middleware like express-jwt you can wrap it in your own middleware like this, and catch it's errors (in my example I clear the cookie I retrieve the token from):

  app.use((req, res, next) => {
    const handleErrorNext = err => {
      if (err) {
        if (
          err.name === 'UnauthorizedError' &&
          err.inner.name === 'TokenExpiredError'
        ) {
          res.clearCookie('auth0idToken');
          return next();
        }
      }
      next(err);
    };
    const middleware = jwt({
      // ...jwt-express options
    });

    middleware(req, res, handleErrorNext);
  });

@mcblum
Copy link

mcblum commented May 24, 2019

@hobofan this may be a dumb question, but how do you error out from there? Basically I was to say if unauthorized return a 403. If I'm not in the callback I can just throw an HttpException but as soon as I'm in my secondary middleware it doesn't work - it throws for the application but is not handled and the request is simply canceled.

Edit: scratch that! It was as simple as res.status(401).json({});

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

4 participants