From 8ae0d768478770972f8e602fb7ecb87a723edc39 Mon Sep 17 00:00:00 2001 From: Urvish Date: Mon, 16 Nov 2026 15:19:54 +0530 Subject: [PATCH 1/2] fix(auth): prevent duplicate phone numbers when updating user profiles --- app/features/auth/service.py | 7 +++++++ tests/api/test_auth_api.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/features/auth/service.py b/app/features/auth/service.py index 02194f1..aefa682 100644 --- a/app/features/auth/service.py +++ b/app/features/auth/service.py @@ -222,6 +222,13 @@ class AuthService: ) country = request.country.strip() if request.country else None + if phone: + existing_user = await self.repository.get_user_by_phone(phone) + if existing_user is not None and str(existing_user.id) != str(user_id): + raise UserAlreadyExistsError( + "An account with this phone number already exists." + ) + user = await self.repository.update_profile( user_id=user_id, display_name=full_name, diff --git a/tests/api/test_auth_api.py b/tests/api/test_auth_api.py index e818151..ceab9ed 100644 --- a/tests/api/test_auth_api.py +++ b/tests/api/test_auth_api.py @@ -187,3 +187,22 @@ async def test_change_password_same_password_returns_400(async_client: AsyncClie assert response.status_code == 400 assert "New password cannot be the same as current password" in response.json()["detail"] + +@pytest.mark.asyncio +async def test_update_profile_duplicate_phone_returns_400(async_client: AsyncClient, mock_auth_service: MagicMock) -> None: + mock_auth_service.update_profile = AsyncMock(side_effect=UserAlreadyExistsError("An account with this phone number already exists.")) + app.dependency_overrides[get_auth_service] = lambda: mock_auth_service + + payload = { + "full_name": "Dr. User Two", + "phone_number": "+8809988776655", + "country": "Bangladesh", + } + headers = {"Authorization": "Bearer sample_valid_token"} + response = await async_client.put("/api/auth/profile", json=payload, headers=headers) + app.dependency_overrides.pop(get_auth_service, None) + + assert response.status_code == 400 + assert "already exists" in response.json()["detail"] + + From 2812dc0ac774207d96899d30054fd4c7bd3a074e Mon Sep 17 00:00:00 2001 From: Urvish Date: Mon, 16 Nov 2026 16:13:04 +0530 Subject: [PATCH 2/2] fix(auth): enforce exact phone matching to prevent substring duplicate errors --- app/features/auth/repository.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/features/auth/repository.py b/app/features/auth/repository.py index 1693b31..475d564 100644 --- a/app/features/auth/repository.py +++ b/app/features/auth/repository.py @@ -42,10 +42,9 @@ class AuthRepository: self, phone_number: str, ) -> AppUser | None: - # Preserves the existing auth API's LIKE lookup semantics. result = await self._session.execute( select(AppUser) - .where(AppUser.phone_number.like(f"%{phone_number}%")) + .where(AppUser.phone_number == phone_number) .order_by(AppUser.id.asc()) .limit(1) )