I have an async function:
const jwtDecode = async (token) => {
//return jsonwebtoken.verify(token, TOKEN_SECRET)
console.log("jwt decode, token: " + token)
try {
const { payload } = await jose.jwtVerify(token, new TextEncoder().encode(TOKEN_SECRET))
return payload
}
catch (e) {
console.log("Caught error jwt decode")
console.error(e.message, e.stack)
return {}
}
}
That function is wrapped by a few other async functions, where await is used.
const foo = async (token) => {
const {userId} = await jwtDecode(token)
return userId
}
const foo2 = async (token) => {
const userId = await foo(token)
return userId
}
const userId = await foo2(token)
When an exception is thrown in jwtDecode, then the whole chain seems to forget about await and executes as if it didn't exist. On top of that, the call to foo2 returns a Promise instead of 'undefined'
I know there is a logical explanation to this. Could someone explain and provide the solution? Thanks
UPDATE:
Here is the full code:
This first call happens in my _middleware.js (I am using Next.js)
export async function middleware(req, ev) {
...
...
...
const userId = await serverUtils.authorizeRequest(req)
...
...
...
const authorizeRequest = async (req) => {
const accessToken = req.cookies.PictosAT
let { userId } = await jwtDecode(accessToken)
if (!userId) {
userId = null
}
console.log("Authorize request userId is " + userId)
return userId
}
const jwtDecode = async (token) => {
console.log("jwt decode, token: " + token)
try {
const { payload } = await jose.jwtVerify(token, new TextEncoder().encode(TOKEN_SECRET))
return payload
}
catch (e) {
console.log("Caught error jwt decode")
console.error(e.message, e.stack)
return {}
}
}