17 lines
504 B
TypeScript
Raw Normal View History

2024-10-14 00:02:58 +08:00
import process from "node:process"
2024-10-14 01:20:43 +08:00
import { jwtVerify } from "jose"
2024-10-14 00:02:58 +08:00
2024-10-14 00:33:37 +08:00
export default defineEventHandler(async (event) => {
2024-10-14 00:02:58 +08:00
const token = getCookie(event, "jwt")
if (token && process.env.JWT_SECRET) {
2024-10-14 01:20:43 +08:00
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 {
2024-10-14 00:02:58 +08:00
logger.error("JWT verification failed")
}
}
})