feat: add bearerAuth Hono middleware

This commit is contained in:
2026-06-16 00:15:48 -06:00
parent c6b41f992a
commit 7e3ca7798e

View File

@@ -0,0 +1,21 @@
import type { MiddlewareHandler } from 'hono'
import { verifyAccessToken } from '../auth'
export type AuthVariables = { userId: string; role: string }
export function bearerAuth(secret: string): MiddlewareHandler<{ Variables: AuthVariables }> {
return async (c, next) => {
const auth = c.req.header('Authorization')
if (!auth?.startsWith('Bearer ')) {
return c.json({ error: '未授权' }, 401)
}
const token = auth.slice(7)
const payload = await verifyAccessToken(token, secret)
if (!payload) {
return c.json({ error: '未授权' }, 401)
}
c.set('userId', payload.userId)
c.set('role', payload.role)
await next()
}
}