feat: add bearerAuth Hono middleware
This commit is contained in:
1 parent
c6b41f992a
commit
7e3ca7798e
1 file changed
+21
@@ -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