feat: implement authentication middleware and routes

- Added `auth.ts` for JWT authentication middleware.
- Created `auth` route to handle authentication and token verification.
- Integrated authentication middleware into `media.ts` for protected routes.
- Updated `main.ts` to register the new authentication route.
This commit is contained in:
ethan.chen
2025-06-23 16:57:22 +08:00
parent e511ab9db6
commit 32f7b86f28
5 changed files with 59 additions and 9 deletions

20
routes/auth.ts Normal file
View File

@@ -0,0 +1,20 @@
import { Hono } from 'hono'
import type { JwtVariables } from 'hono/jwt'
import { authMiddleware } from '../auth.ts'
const auth = new Hono<{ Variables: JwtVariables }>()
auth.use('/*', authMiddleware)
auth.get('/verify', (c) => {
const payload = c.get('jwtPayload')
return c.json({
code: 0,
data: {
username: payload.username,
},
message: '认证成功',
})
})
export default auth

View File

@@ -1,9 +1,12 @@
import { Hono } from 'hono'
import { db } from '../db/index.ts'
import type { JwtVariables } from 'hono/jwt'
import { authMiddleware } from '../auth.ts'
const media = new Hono<{ Variables: JwtVariables }>()
media.use('/*', authMiddleware)
// 获取所有媒体记录
media.get('/list', (c) => {
try {