mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-02-07 22:10:03 +08:00
21 lines
689 B
TypeScript
21 lines
689 B
TypeScript
import process from "node:process"
|
|
import { jwtVerify } from "jose"
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
if (["JWT_SECRET", "G_CLIENT_ID", "G_CLIENT_SECRET"].find(k => !process.env[k])) {
|
|
event.context.user = true
|
|
} else {
|
|
const token = getHeader(event, "Authorization")
|
|
if (token && process.env.JWT_SECRET) {
|
|
try {
|
|
const { payload } = await jwtVerify(token.replace("Bearer ", ""), 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")
|
|
}
|
|
}
|
|
}
|
|
})
|