Issue
I'm writing my first expressJs app using TypeScript. I got got static middleware method for token validation, where I need pass data to the next middleware:
static verifyAccessToken(req: Request, resp: Response, next: NextFunction) {
const AUTH_TOKEN = AccessTokenValidator.getTokenFromHeader(req, resp);
jwt.verify(AUTH_TOKEN, Config.JWT_SECRET, async (error: VerifyErrors | null, payload: any) => {
if (error) {
resp.status(401).send({message: "Token is invalid"});
}
// @ts-ignore
req.userRole = payload.rol;
next();
});
}
How can I pass data to next middleware correctly, without using "@ts-ignore"?
Solution
You can add custom express request type definition by creating a .d.ts
file
Create express.d.ts
on your root project folder, and put
declare namespace Express {
export interface Request {
userRole?: string // I use string for example, you can put other type
}
}
Answered By - Owl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.