2024-10-14 01:20:43 +08:00

17 lines
504 B
TypeScript

import process from "node:process"
import { jwtVerify } from "jose"
export default defineEventHandler(async (event) => {
const token = getCookie(event, "jwt")
if (token && process.env.JWT_SECRET) {
try {
const { payload } = await jwtVerify(token, new TextEncoder().encode(process.env.JWT_SECRET)) as { payload?: { id: string, type: string } }
if (payload?.id) {
event.context.user = payload.id
}
} catch {
logger.error("JWT verification failed")
}
}
})