im currently working on a express application that uses typescript. Im currently working on a Authentication Middleware and was wondering if you can make the Middlewares typesafe in a way:
authenticateJwt = (
req: RequestWithToken,
res: Response,
next: () => void
) => {
// Append the decoded req.token to the req header so we can use it internally
const token = req.token;
// @ts-ignore
this.verifyJwt(token)
.then((decoded: Token) => {
req.token = decoded;
next();
})
.catch(() => res.status(401).send('Unauthorized'));
};
now in my routes.ts:
router.get(
'/me',
// @ts-ignore
jwtService.authenticateJwt,
userController.getProfileFromUser
);
I have to write // @ts-ignore because it says that '(req: RequestWithToken, res: Response, next: () => void) => void
is not of type RequestHandlerParams
definition of RequestWithToken
:
export interface RequestWithToken extends Request {
token: Token;
}
export interface Token {
username: string;
}