Browse Source

配置了 openapi 的 auth

yourname 6 months ago
parent
commit
9d44a00a40
4 changed files with 19 additions and 10 deletions
  1. 0 1
      src/server/api.ts
  2. 3 7
      src/server/api/auth.ts
  3. 7 1
      src/server/api/user.ts
  4. 9 1
      src/server/middleware/auth.middleware.ts

+ 0 - 1
src/server/api.ts

@@ -10,7 +10,6 @@ import { authOpenApiApp } from './api/auth'
 
 const createApi = (app:OpenAPIHono) => {
 
-  // app.use('/api/v1/*', authMiddleware)
   app.onError(errorHandler)
 
   // Rate limiting

+ 3 - 7
src/server/api/auth.ts

@@ -4,13 +4,9 @@ import { UserService } from '../modules/users/user.service'
 import { User } from '../modules/users/user.entity'
 import { z } from 'zod'
 import { HTTPException } from 'hono/http-exception'
+import { AuthContext, authMiddleware } from '../middleware/auth.middleware'
+
 
-// 扩展Context类型
-type AuthContext = {
-  Variables: {
-    user: User
-  }
-}
 
 const app = new OpenAPIHono<AuthContext>()
 const authService = new AuthService()
@@ -115,7 +111,7 @@ app.openapi(registerRoute, async (c) => {
 const meRoute = createRoute({
   method: 'get',
   path: '/auth/me',
-  security: [{ Bearer: [] }],
+  middleware: authMiddleware,
   responses: {
     200: {
       description: '获取成功',

+ 7 - 1
src/server/api/user.ts

@@ -1,8 +1,9 @@
 import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
 import { UserService } from '../modules/users/user.service';
 import { z } from 'zod';
+import { AuthContext , authMiddleware } from '../middleware/auth.middleware';
 
-const app = new OpenAPIHono()
+const app = new OpenAPIHono<AuthContext>()
 const userService = new UserService();
 
 const UserSchema = z.object({
@@ -30,6 +31,7 @@ const UpdateUserSchema = CreateUserSchema.partial();
 const createUserRoute = createRoute({
   method: 'post',
   path: '/users',
+  middleware: authMiddleware,
   request: {
     query: CreateUserSchema
   },
@@ -56,6 +58,7 @@ const createUserHandler = app.openapi(createUserRoute, async (c) => {
 const listUsersRoute = createRoute({
   method: 'get',
   path: '/users',
+  middleware: authMiddleware,
   responses: {
     200: {
       description: 'Success',
@@ -88,6 +91,7 @@ const listUsersHandler = app.openapi(
 const getUserRoute = createRoute({
   method: 'get',
   path: '/users/{id}',
+  middleware: authMiddleware,
   request: {
     params: z.object({
       id: z.string().openapi({ example: '1' })
@@ -120,6 +124,7 @@ const getUserHandler = app.openapi(
 const updateUserRoute = createRoute({
   method: 'patch',
   path: '/users/{id}',
+  middleware: authMiddleware,
   request: {
     params: z.object({
       id: z.string().openapi({ example: '1' })
@@ -160,6 +165,7 @@ const updateUserHandler = app.openapi(
 const deleteUserRoute = createRoute({
   method: 'delete',
   path: '/users/{id}',
+  middleware: authMiddleware,
   request: {
     params: z.object({
       id: z.string().openapi({ example: '1' })

+ 9 - 1
src/server/middleware/auth.middleware.ts

@@ -1,8 +1,16 @@
 import { Context, Next } from 'hono';
 import { AuthService } from '../modules/auth/auth.service';
 import { UserService } from '../modules/users/user.service';
+import { User } from '../modules/users/user.entity';
 
-export async function authMiddleware(c: Context, next: Next) {
+// 扩展Context类型
+export type Variables = {
+  user: User
+}
+
+export type AuthContext = { Variables: Variables }
+
+export async function authMiddleware(c: Context<AuthContext>, next: Next) {
   try {
     const authHeader = c.req.header('Authorization');
     if (!authHeader) {