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?
Hi @cdiaz,
Just use class middleware, not the functional one.
Hi @kamilmysliwiec, i'm using class middleware, but I can not access to the next() function inside passport.

This is my Example
Result:
TypeError: next is not a function
Hi @cdiaz ,
Change:
 (err, user, info, next) =>
into:
 (err, user, info) =>
You're overriding next variable.
@kamilmysliwiec I had already tried that but it does not work.
next() does not exist in the passport context

Hey !
If you try to wrap you return in a arrow function like that ?
return async (req: Request, res: Response, next: NextFunction) => {
Hi @adrien2p
something like that?
@Middleware()
export class JwtMiddleware implements NestMiddleware {
  public resolve(): (req, res, next) => void {
    return async (req: Request, res: Response, next: NextFunction) => {
      await passport.authenticate('jwt', { session: false }, (err, user, info) => {
        if(err){
          throw new HttpException(info.message, 401)
        }
        else{
          next()
        }
      })
    }
  }
}
Hi @cdiaz yes something like that, did you try ?
@adrien2p Yes, I tried but Next() does not work within the passport context
Hey, could you try insteadof the third parameter something like
passeport.authenticate('jwt', { session: false })(req, res, next)
Then ensure you than the header contains 'Authorization JWT Jwt_token and let me noticed :)
See you
thanks @adrien2p , but I have already tried all this and I have not been able to solve it, @kamilmysliwiec reopen the issue until there is a solution, please
@Middleware()
export class JwtMiddleware implements NestMiddleware {
  public resolve(): (req, res, next) => void {
    return passport.authenticate('jwt', { session: false })
  }
}
Could you execute that and send back the exception handled ?
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)
        }
    }
}
Hey ! I've already propose this solution which apparently doesnt work ...
each example is missing some parts that's why its does not work.
Yes you right i didn't write the callback ;)
Hi @cdiaz,
Is the @darioxtx's example works for you :)?
Hi @cdiaz,
Let us know if you still have any issues 馃檪 
Yes,  thanks @kamilmysliwiec, @darioxtx this solution worked for me.
the Middleware has been updated
@cdiaz Your example doesn't seem to work if I change this line: https://github.com/cdiaz/nest-passport/blob/master/src/auth/middlewares/login.middleware.ts#L8
from {session: false} to {session: true}
Am I missing something?
guys i am having the same probleme it says
TypeError: next is not a function
 can you help me solve it
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.