mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 11:19:14 +08:00
17 lines
466 B
TypeScript
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")
|
|
}
|
|
}
|
|
})
|