129 lines
4.1 KiB
Markdown
129 lines
4.1 KiB
Markdown
# 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**:
|
|
1. User fills signup form and clicks "Sign Up"
|
|
2. App calls `checkUser()` API to verify if phone number is registered
|
|
3. If user exists → Show message and navigate to sign-in
|
|
4. 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-user` endpoint to check if user has a name (fully registered)
|
|
- Returns `user_exists: true` only if:
|
|
- User exists in database
|
|
- User has a name (not just created by verify-otp)
|
|
- Returns `user_exists: false` if:
|
|
- User doesn't exist, OR
|
|
- User exists but has no name (incomplete signup - allow them to continue)
|
|
|
|
**Logic**:
|
|
```javascript
|
|
// 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
|
|
1. User enters phone number that doesn't exist in database
|
|
2. Clicks "Sign Up"
|
|
3. System checks → User doesn't exist
|
|
4. Proceeds with OTP request and signup flow ✅
|
|
|
|
### Scenario 2: Fully Registered User
|
|
1. User enters phone number that exists and has a name
|
|
2. Clicks "Sign Up"
|
|
3. System checks → User exists and is fully registered
|
|
4. Shows toast: "This phone number is already registered. Please sign in instead."
|
|
5. Automatically navigates to sign-in screen ✅
|
|
|
|
### Scenario 3: Incomplete Signup
|
|
1. User previously started signup (verify-otp created user but didn't complete)
|
|
2. User enters same phone number again
|
|
3. Clicks "Sign Up"
|
|
4. System checks → User exists but has no name
|
|
5. Proceeds with signup to complete registration ✅
|
|
|
|
## Benefits
|
|
|
|
1. **Prevents Duplicate Accounts**: Users can't create multiple accounts with the same phone number
|
|
2. **Better UX**: Clear message directing users to sign in if already registered
|
|
3. **Handles Edge Cases**: Users who started but didn't complete signup can still finish
|
|
4. **Automatic Navigation**: Seamlessly redirects to sign-in screen
|
|
|
|
## Testing
|
|
|
|
To test the implementation:
|
|
|
|
1. **Test New User Signup**:
|
|
- Enter a new phone number
|
|
- Should proceed with signup normally
|
|
|
|
2. **Test Existing User**:
|
|
- Enter a phone number that's already registered
|
|
- Should show message and navigate to sign-in
|
|
|
|
3. **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)
|
|
```json
|
|
{
|
|
"success": true,
|
|
"message": "User is already registered. Please sign in instead.",
|
|
"user_exists": true
|
|
}
|
|
```
|
|
|
|
### User Doesn't Exist or Incomplete
|
|
```json
|
|
{
|
|
"success": false,
|
|
"error": "USER_NOT_FOUND",
|
|
"message": "User is not registered. Please sign up to create a new account.",
|
|
"user_exists": false
|
|
}
|
|
```
|
|
|