2024-10-14 00:02:58 +08:00
|
|
|
import process from "node:process"
|
2024-10-14 00:33:37 +08:00
|
|
|
import jwt from "@tsndr/cloudflare-worker-jwt"
|
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 00:33:37 +08:00
|
|
|
const v = await jwt.verify(token, process.env.JWT_SECRET) as { preload?: { id: string, exp: number } }
|
|
|
|
if (v?.preload?.id) {
|
|
|
|
event.context.user = v.preload.id
|
|
|
|
} else {
|
2024-10-14 00:02:58 +08:00
|
|
|
logger.error("JWT verification failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|