2024-10-14 00:02:58 +08:00

17 lines
466 B
TypeScript

import process from "node:process"
import jwt from "jsonwebtoken"
export default defineEventHandler((event) => {
const token = getCookie(event, "jwt")
if (token && process.env.JWT_SECRET) {
try {
const { id: userID, exp } = jwt.verify(token, process.env.JWT_SECRET) as { id: string, exp: number }
if (Date.now() < exp * 1000) {
event.context.user = userID
}
} catch {
logger.error("JWT verification failed")
}
}
})