4.1 KiB
4.1 KiB
Signup Duplicate Phone Number Check
Problem
When a user tries to sign up with a phone number that is already registered in the database, they should be shown a message directing them to sign in instead of proceeding with signup.
Solution Implemented
1. Updated SignUpScreen (Android App)
File: LivingAi_Lg/app/src/main/java/com/example/livingai_lg/ui/screens/auth/SignUpScreen.kt
Changes:
- Added a check before requesting OTP to verify if the user already exists
- If user exists and is fully registered (has a name), shows a toast message: "This phone number is already registered. Please sign in instead."
- Automatically navigates to the sign-in screen
- If user doesn't exist or is in the middle of signup (no name), proceeds with normal signup flow
Flow:
- User fills signup form and clicks "Sign Up"
- App calls
checkUser()API to verify if phone number is registered - If user exists → Show message and navigate to sign-in
- If user doesn't exist → Proceed with OTP request and signup
2. Enhanced check-user Endpoint (Backend)
File: farm-auth-service/src/routes/authRoutes.js
Changes:
- Updated the
/auth/check-userendpoint to check if user has a name (fully registered) - Returns
user_exists: trueonly if:- User exists in database
- User has a name (not just created by verify-otp)
- Returns
user_exists: falseif:- User doesn't exist, OR
- User exists but has no name (incomplete signup - allow them to continue)
Logic:
// Check if user exists and has a name (fully registered)
const found = await db.query(
`SELECT id, name FROM users
WHERE (phone_number = $1 OR phone_number = $2)
AND deleted = FALSE`,
phoneSearchParams
);
if (found.rows.length === 0) {
// User not found - allow signup
return { user_exists: false };
}
const user = found.rows[0];
const isFullyRegistered = user.name && user.name.trim() !== '';
if (isFullyRegistered) {
// User is fully registered - should sign in
return { user_exists: true, message: 'User is already registered. Please sign in instead.' };
} else {
// User exists but incomplete - allow signup to continue
return { user_exists: false };
}
User Experience
Scenario 1: New User
- User enters phone number that doesn't exist in database
- Clicks "Sign Up"
- System checks → User doesn't exist
- Proceeds with OTP request and signup flow ✅
Scenario 2: Fully Registered User
- User enters phone number that exists and has a name
- Clicks "Sign Up"
- System checks → User exists and is fully registered
- Shows toast: "This phone number is already registered. Please sign in instead."
- Automatically navigates to sign-in screen ✅
Scenario 3: Incomplete Signup
- User previously started signup (verify-otp created user but didn't complete)
- User enters same phone number again
- Clicks "Sign Up"
- System checks → User exists but has no name
- Proceeds with signup to complete registration ✅
Benefits
- Prevents Duplicate Accounts: Users can't create multiple accounts with the same phone number
- Better UX: Clear message directing users to sign in if already registered
- Handles Edge Cases: Users who started but didn't complete signup can still finish
- Automatic Navigation: Seamlessly redirects to sign-in screen
Testing
To test the implementation:
-
Test New User Signup:
- Enter a new phone number
- Should proceed with signup normally
-
Test Existing User:
- Enter a phone number that's already registered
- Should show message and navigate to sign-in
-
Test Incomplete Signup:
- Start signup but don't complete (verify OTP but don't finish)
- Try to sign up again with same number
- Should allow completion of signup
API Response Examples
User Exists (Fully Registered)
{
"success": true,
"message": "User is already registered. Please sign in instead.",
"user_exists": true
}
User Doesn't Exist or Incomplete
{
"success": false,
"error": "USER_NOT_FOUND",
"message": "User is not registered. Please sign up to create a new account.",
"user_exists": false
}