Learning about Typescript has been quite a challenge for me, especially when it comes to using the correct syntax.
I have implemented a promise to retrieve decoded content from jwt.verify - jsonwebtoken. It is functioning as intended and providing an object with user.id, iat, and expiry information. However, I encountered a type error on the resolve promise that states: "Argument of type 'object' is not assignable to parameter of type 'IVerifiedUserType | PromiseLike | undefined'."
Below you will find the Interface and code snippet that I am working with. I have utilized async await in handling the promise.
export interface IVerifiedUserType {
id: number;
iat: number;
exp: number;
}
const verifyToken = (token: string, config: IConfigType): Promise<IVerifiedUserType> =>
new Promise((resolve, reject) => {
if (config.secrets.jwt) {
jwt.verify(token, config.secrets.jwt, (err, decoded) => {
if (err) {
return reject(err);
}
if (typeof decoded === "object") {
resolve(decoded);
}
});
}
});
const verifiedToken = await authService.verifyToken(token, config);
I am utilizing "jsonwebtoken": "^8.5.1", and "@types/jsonwebtoken": "^8.3.3", for types definitions.