浏览代码

♻️ refactor(auth): 迁移认证中间件到独立模块

- 删除本地认证中间件文件,改为从 @d8d/auth-module 导入
- 在支付创建路由中添加认证中间件使用
- 更新 pnpm 依赖配置,添加 auth-module 依赖
yourname 3 周之前
父节点
当前提交
caac8a8745

+ 0 - 35
packages/mini-payment/src/middleware/auth.middleware.ts

@@ -1,35 +0,0 @@
-import { createMiddleware } from 'hono/factory';
-
-/**
- * 认证中间件
- * 用于验证用户身份并获取用户信息
- */
-export const authMiddleware = createMiddleware(async (c, next) => {
-  try {
-    // 从认证模块获取用户信息
-    const user = c.get('user');
-
-    if (!user) {
-      return c.json({
-        message: '未授权访问'
-      }, 401);
-    }
-
-    // 设置用户OpenID到上下文
-    const userOpenid = user.openid;
-    if (!userOpenid) {
-      return c.json({
-        message: '用户未绑定微信小程序'
-      }, 400);
-    }
-
-    c.set('userOpenid', userOpenid);
-
-    await next();
-  } catch (error) {
-    console.error('认证中间件错误:', error);
-    return c.json({
-      message: '认证失败'
-    }, 401);
-  }
-});

+ 0 - 1
packages/mini-payment/src/middleware/index.ts

@@ -1 +0,0 @@
-export { authMiddleware } from './auth.middleware.js';

+ 2 - 0
packages/mini-payment/src/routes/payment/create.ts

@@ -1,6 +1,7 @@
 import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
 import { z } from 'zod';
 import { AppDataSource } from '@d8d/shared-utils';
+import { authMiddleware } from '@d8d/auth-module';
 import { PaymentService } from '../../services/payment.service.js';
 import { PaymentCreateRequestSchema, PaymentCreateResponseSchema } from '../../schemas/payment.schema.js';
 
@@ -36,6 +37,7 @@ const createPaymentRoute = createRoute({
 });
 
 const app = new OpenAPIHono()
+  .use(authMiddleware)
   .openapi(createPaymentRoute, async (c) => {
     try {
       const paymentData = c.req.valid('json');

文件差异内容过多而无法显示
+ 474 - 0
pnpm-lock.yaml


部分文件因为文件数量过多而无法显示