From 95c27f6848907c5ce8d89bca3a624fe5093b9976 Mon Sep 17 00:00:00 2001 From: Claude AI Date: Wed, 18 Mar 2026 06:30:24 +0000 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E6=B7=BB=E5=8A=A0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=B3=A8=E5=86=8C=20API=20=E4=BB=A3=E7=90=86=E7=AB=AF?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 /api/auth/register 端点代理用户注册请求 - 支持邮箱、用户名、密码注册 - 集成 Novel Platform 用户注册 API - 完善用户认证功能体系 Co-Authored-By: Claude Opus 4.6 --- backend/app_fastapi.py | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/backend/app_fastapi.py b/backend/app_fastapi.py index e094d50..03d48d3 100644 --- a/backend/app_fastapi.py +++ b/backend/app_fastapi.py @@ -623,6 +623,78 @@ async def admin_login(request: Request): raise HTTPException(status_code=500, detail=str(e)) +@app.post("/api/auth/register") +async def register(request: Request): + """ + Novel Platform 用户注册 + 代理到实际的注册端点 + """ + try: + data = await request.json() + email = data.get('email') + username = data.get('username') + password = data.get('password') + + if not email or not username or not password: + raise HTTPException(status_code=400, detail="Email, username and password are required") + + # 查找用户 MCP 服务器 + target_server = MCP_SERVERS.get('novel-platform-user') + if not target_server: + # 如果没有专门的用户服务器,尝试找到任何需要 JWT 认证的服务器 + for server_id, config in MCP_SERVERS.items(): + if config.get('auth_type') == 'jwt' and 'base_url' in config: + if 'user' in server_id: + target_server = config + break + elif target_server is None: + target_server = config + + if not target_server: + raise HTTPException(status_code=400, detail="No JWT-authenticated server configured") + + # 构建注册 URL + base_url = target_server.get('base_url', '') + register_url = f"{base_url}/api/v1/auth/register" + + # 调用实际的注册接口(异步版本) + async with httpx.AsyncClient(timeout=30.0) as http_client: + response = await http_client.post( + register_url, + json={"email": email, "username": username, "password": password} + ) + + if response.status_code in (200, 201): + result = response.json() + # Novel Platform API 返回用户对象 + return { + "success": True, + "message": "注册成功", + "user": { + "id": result.get("id"), + "email": result.get("email"), + "username": result.get("username"), + "role": result.get("role") + } + } + else: + # 尝试解析错误响应 + try: + error_detail = response.json() + error_msg = error_detail.get("detail", response.text) + except: + error_msg = response.text + raise HTTPException( + status_code=response.status_code, + detail=error_msg + ) + + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + @app.post("/api/auth/logout") async def logout(request: Request): """登出并清除会话"""