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): """登出并清除会话"""