Reputation: 163
I'm trying to figure out why this code:
const extractJWT = (req: Request, res: Response, next: NextFunction) => {
logging.info(NAMESPACE, "Validating Token");
let token = req.headers.authorization?.split(" ")[1];
if (token) {
jwt.verify(token, config.server.token.secret, (error, decoded) => {
if (error) {
return res.status(404).json({
message: error.message,
error,
});
} else {
res.locals.jwt = decoded;
next();
}
});
} else {
return res.status(401).json({
message: "Unauthorized",
});
}
};
export default extractJWT;
is throwing this error:
TSError: ⨯ Unable to compile TypeScript
src/middleware/extractJWT.ts (11,43): Expression expected. (1109)
src/middleware/extractJWT.ts (11,57): ':' expected. (1005)
I'm trying to run extractJwt function as middleware on a Controller. I'm using routing-controller @UseBefore decorator on the action. Without the decorator, I don't get the error. With it, I do. I have no clue how it suddenly changes. I'd appreciate the any help here.
@Get("/validate")
@UseBefore(extractJWT)
async validate(@Req() req: Request, @Res() res: Response, next: NextFunction) {
logging.info(NAMESPACE, "Token Validated");
return res.status(200).json({
message: "Authorized",
});
}
Upvotes: 0
Views: 650