Currently, I am in the process of working with JWT and have a function set up as seen below:
export async function decodeJwt(token: string): Promise<string> {
console.log('token is a string: ', typeof token === 'string');
const payload = await jwt.verify(token, RSA_PUBLIC_KEY);
console.log('payload: ', payload);
return payload;
}
However, despite the fact that this function should be receiving a string for the token
variable, it turns out that when called, it is not actually a string. The initial line of code indicates that token
does not fit the criteria of being a string.
This raises the question - how can this even happen?
The issue at hand arises from the fact that upon calling jwt.verify()
, an error is triggered:
jwt must be a string
What's puzzling about this error is the fact that when looking at the following block of code where decodeJwt()
is invoked with a supposedly string value for token
:
async function handleSessionCookie(token: string, req: Request) {
try {
const payload = await decodeJwt(token);
console.log('payload: ', payload);
req['userId'] = payload.sub;
} catch (err) {
console.log('handleSessionCookie Error: Could not extract user from the request: ', err.message);
}
}
Everything seems to align with strings.
So, what could possibly be causing this inconsistency?