OTP & Phone Verification
The RideEase authentication system utilizes Firebase Phone Authentication to verify user identities via One-Time Passwords (OTP). This process is primarily managed through the UserDetailsViewModel, which interacts with an underlying AuthRepository to handle the asynchronous verification flow.
Phone Verification Flow
The verification process follows a two-step handshake:
- Request OTP: The application sends the user's phone number to Firebase.
- Submit OTP: The user enters the 6-digit code received via SMS to complete the verification and update their profile.
Initiating Verification
To start the process, use the startPhoneVerification method. This method triggers the SMS delivery to the provided phone number.
Method Signature
public void startPhoneVerification(String phoneNumber)
| Parameter | Type | Description |
| :--- | :--- | :--- |
| phoneNumber | String | The user's phone number in E.164 format (e.g., +11234567890). |
Usage Example
// In your Activity or Fragment
String fullNumber = countryCode + phoneNumber;
userDetailsViewModel.startPhoneVerification(fullNumber);
Verifying the OTP Code
Once the user receives the 6-digit SMS code, it must be submitted along with the verificationId received from the initial request.
Method Signature
public void verifyPhoneCode(
String verificationId,
String code,
String userId,
String fullName,
String phoneNumber
)
| Parameter | Type | Description |
| :--- | :--- | :--- |
| verificationId | String | The ID generated by Firebase when the code was sent. |
| code | String | The 6-digit OTP entered by the user. |
| userId | String | The unique UID of the authenticated Firebase user. |
| fullName | String | The user's full name to be stored in the profile. |
| phoneNumber | String | The verified phone number. |
Usage Example
userDetailsViewModel.verifyPhoneCode(
currentVerificationId,
"123456",
firebaseUser.getUid(),
"John Doe",
"+11234567890"
);
Observing Verification State
The UserDetailsViewModel exposes several LiveData objects to allow the UI to react to the verification lifecycle.
| LiveData | Type | Description |
| :--- | :--- | :--- |
| getVerificationId() | LiveData<String> | Emits the verificationId once the OTP has been successfully sent by Firebase. |
| getAuthResult() | LiveData<Boolean> | Emits true when the OTP verification is successful and user details are saved. |
| getErrorMessage() | LiveData<String> | Emits a human-readable error message if validation or the network request fails. |
Data Validation
The system enforces strict validation rules before attempting any network requests to ensure API efficiency and data integrity.
Phone Number Requirements
The application validates phone numbers using the following regex pattern:
^\+[0-9]{10,15}$
- Must include a
+prefix. - Must include the country code.
- Must be between 10 and 15 digits in length.
OTP Requirements
- The code must be exactly 6 digits.
- It cannot be empty or contain non-numeric characters.
User Profile Integration
Upon successful phone verification, the user's data is updated in the Firestore users collection. The User model reflects the verification status:
// User.java relevant fields
public class User {
private String phoneNumber;
private boolean isPhoneVerified; // Set to true after successful OTP verification
// ...
}
Post-Verification Navigation
The AuthUtils helper provides a checkUserDetails method to determine if a user has completed their profile (including phone verification). This is used during the splash screen or login flow to route users correctly:
AuthUtils.checkUserDetails(userId, new AuthUtils.UserDetailsCallback() {
@Override
public void onComplete(boolean hasDetails, String userId) {
if (hasDetails) {
// Navigate to Home
} else {
// Navigate to UserDetailsActivity to complete OTP verification
}
}
});