Can anyone help me figure out how to correctly define a return value for an express middleware that utilizes async/await? I've been experimenting with different approaches but haven't found success yet.
Additionally, I'm attempting to extend the Request object in order to set req.user after decoding the JWT payload.
import { NextFunction, Request, RequestHandler, Response } from 'express'
const authenticate: RequestHandler = async (req: IUserRequest, res: Response, next: NextFunction): Promise<void> => {
if (! req.headers.authorization) { return next(new ValidationError()) }
const payload: IUserData = await decodeJwt(req.headers.authorization)
req.user = payload
return next()
}
export interface IUserRequest extends Request {
user: IUserData
}
/* errors
(req: IUserRequest, res: Response, next: NextFunction) => Promise<void>' is not assignable to type 'RequestHandler'.
Types of parameters 'req' and 'req' are incompatible.
Type 'Request' is not assignable to type 'IUserRequest'.
Property 'user' is missing in type 'Request'.
*/