feat: add bearerAuth Hono middleware
This commit is contained in:
21
server/middleware/bearerAuth.ts
Normal file
21
server/middleware/bearerAuth.ts
Normal 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user