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.

[Passport] Custom callback in Passport Middleware for error handler

See original GitHub issue

Some errors types as invalid signature, jwt expired, no auth token, invalid token, invalid algorithm… are not being handled on passport-jwt

The default response send only ‘unauthorized’ message and http status 401

In this Issue the author recommend using custom callbacks according to official passport docs

I’ve tried to try the following code, but I can not get it to work fine

export class AuthModule implements NestModule {
  public configure(consumer: MiddlewaresConsumer) {
    consumer
      .apply(passport.authenticate('jwt', { session: false }, (err, user, info) => {
        console.log(info.message)
      }))
      .forRoutes({ path: '/auth/authorized', method: RequestMethod.ALL });
  }
}

console.log(info.message) Is printed but does not can exit function, and within it next() function is not available, so response stays in the waiting forever

Any idea?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
darioxtxcommented, Oct 17, 2017

hi,

you can access next like this.

@Middleware()
export class LocalStrategyMiddleware implements NestMiddleware {
    resolve() {
        return async (req, res, next) => {
            return await passport.authenticate('local', {session: true}, (err, user, info) => {
                if (err) {
                    next(new HttpException(err, 401));
                }
                else {
                    next();
                }
            })(req, res, next)
        }
    }
}
1reaction
kamilmysliwieccommented, Oct 7, 2017

Hi @cdiaz, Just use class middleware, not the functional one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express Passport (node.js) error handling - Stack Overflow
The strategy-implementation works in conjunction with passport.authenticate to both authenticate a request, and handle success/failure.
Read more >
how to customize http response if failed authentication? #157
I have below code to use passport-jwt to verify user authentication. But it returns 500 internal error to the client if it failed ......
Read more >
Documentation: Middleware - Passport.js
In this route, passport.authenticate() is middleware which will authenticate the request. By default, when authentication succeeds, the req.user property is set ...
Read more >
How to use the passport.authenticate function in passport - Snyk
To help you get started, we've selected a few passport.authenticate examples, ... Otherwise, // authentication has failed. app.get('/auth/twitter/callback', ...
Read more >
Authenticate Users With Node ExpressJS and Passport.js
Our application contains one POST route, /login. We are ready to make the authentication process work by creating this route. app.post('/login ...
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