auth0 / express-jwt

connect/express middleware that validates a JsonWebToken (JWT) and set the req.user with the attributes
MIT License
4.49k stars 444 forks source link

Add support for express v5 #348

Open quinarygio opened 2 weeks ago

quinarygio commented 2 weeks ago

Add support for Express v5.0

After upgrading to express v5.0.0 the compilation fails with the following error:

app.use(
    /\/((?!healthcheck).)*/, // Token verification activated except for healthcheck request
    expressjwt({
        secret: jwksRsa.expressJwtSecret({
            cache: true,
            rateLimit: true,
            jwksRequestsPerMinute: 5,
            jwksUri: jwksUri
        }) as GetVerificationKey,
        algorithms: ['RS256']
    })
);
 error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type '{ (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction): Promise<...>; unless: (options: Params) => { ...; }; }' is not assignable to parameter of type 'Application<Record<string, any>>'.
      Type '{ (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction): Promise<...>; unless: (options: Params) => { ...; }; }' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.
KillerCodeMonkey commented 2 weeks ago

possible Workaround:

app.use(
    /\/((?!healthcheck).)*/, // Token verification activated except for healthcheck request
    async (req: any, res: any, next: NextFunction) => expressjwt({
        secret: jwksRsa.expressJwtSecret({
            cache: true,
            rateLimit: true,
            jwksRequestsPerMinute: 5,
            jwksUri: jwksUri
        }) as GetVerificationKey,
        algorithms: ['RS256']
    })(req, res, next) as Promise<void>
);

Background: middlewares are considered async now and Request, Response interfaces are a little bit different.

quinarygio commented 2 weeks ago

Thank you for the quick response, but unfortunately the workaround does not solve the issue, the compilation still fails:

error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type '(req: any, res: any, next: NextFunction) => Promise<void | NodeJS.Immediate>' is not assignable to parameter of type 'Application<Record<string, any>>'.
      Type '(req: any, res: any, next: NextFunction) => Promise<void | Immediate>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.
KillerCodeMonkey commented 2 weeks ago

@quinarygio do you have the as Promise<void> in your code?

quinarygio commented 2 weeks ago

@quinarygio do you have the as Promise<void> in your code?

Sorry I was missing it, as I copied from the received email. It works now ! Thank you

KillerCodeMonkey commented 2 weeks ago

Seems like the main-problem is, that it is wrapping calling next with setImmediate so the return type is not matching.