In my node.js project, I have used typescript.
I want to get userId from the result of jwt.verify() but I got an error.
How can I fix this problem? (If I don't use typescript, there is no problem.)
userService.ts file:
In Login method I have the following code:
import jwt from "jsonwebtoken";
// I have defined userId
token = jwt.sign({ userId: userId, email: email }, secretKey, { expiresIn: expiresIn });
check-auth.ts file:
Also in check-auth middleware I have:
import jwt from "jsonwebtoken";
const decodedToken = jwt.verify(token, process.env.SECRET_KEY);
// Property 'userId' does not exist on type 'string | object'.
// Property 'userId' does not exist on type 'string'.ts(2339)
req.userData = { userId: decodedToken.userId }; // I need to access to the userId