From a7f816fe5ba1553690efae97adb0ca45d0113bc2 Mon Sep 17 00:00:00 2001 From: Xiaoxia AI Date: Sun, 21 Jun 2026 16:24:21 +0800 Subject: [PATCH] fix(auth): allow registration without display name --- apps/api/app/api/routes/auth.py | 6 ++++-- apps/web/src/pages/auth/Register.tsx | 1 + tests/unit/test_auth.py | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/api/app/api/routes/auth.py b/apps/api/app/api/routes/auth.py index 261c99c8c..482b566ae 100644 --- a/apps/api/app/api/routes/auth.py +++ b/apps/api/app/api/routes/auth.py @@ -5,6 +5,8 @@ The route layer is intentionally thin: repository construction lives in app.dependencies and authentication behavior lives in application use cases. """ +from typing import Optional + from app.auth import AuthenticatedUser, get_current_user from app.config import settings from app.dependencies import get_auth_email_service, get_auth_session_store, get_user_repository @@ -32,7 +34,7 @@ class RegisterRequest(BaseModel): email: EmailStr password: str username: str - display_name: str + display_name: Optional[str] = None class RegisterResponse(BaseModel): @@ -99,7 +101,7 @@ async def register( email=request.email, password=request.password, username=request.username, - display_name=request.display_name, + display_name=request.display_name or request.username, ) ) if error or response is None: diff --git a/apps/web/src/pages/auth/Register.tsx b/apps/web/src/pages/auth/Register.tsx index 1788faa29..45e9a0cda 100644 --- a/apps/web/src/pages/auth/Register.tsx +++ b/apps/web/src/pages/auth/Register.tsx @@ -25,6 +25,7 @@ const Register: React.FC = () => { email: values.email, username: values.username, password: values.password, + display_name: values.username, }); message.success('注册成功!请查收验证邮件。'); } catch (error: any) { diff --git a/tests/unit/test_auth.py b/tests/unit/test_auth.py index 071e2aae5..fbd8358c5 100644 --- a/tests/unit/test_auth.py +++ b/tests/unit/test_auth.py @@ -14,9 +14,11 @@ if str(API_ROOT) not in sys.path: from app.api.routes.auth import ( PasswordResetRequestModel, + RegisterRequest, ResetPasswordModel, VerifyEmailRequestModel, forgot_password, + register, reset_password, verify_email_post, ) @@ -90,6 +92,21 @@ def test_register_use_case_hashes_password_and_normalizes_email(): assert password_hasher.verify_password("Password1", user.password_hash) +def test_register_route_defaults_display_name_to_username(): + repo = InMemoryUserRepository() + + response = asyncio.run( + register( + RegisterRequest(email="route@example.com", password="Password1", username="routeuser"), + repo, + DummyEmailService(), + ) + ) + + assert response.display_name == "routeuser" + assert repo.find_by_email("route@example.com").display_name == "routeuser" + + def test_login_use_case_returns_verifiable_jwt(): repo = InMemoryUserRepository() session_store = DummySessionStore()