Repository Layer
Repository Layer
The Repository layer acts as an abstraction between the application's business logic (ViewModels) and its data sources (Firebase). By centralizing data access, the repository ensures a clean separation of concerns and facilitates easier unit testing.
AuthRepository
The AuthRepository is the primary component for handling authentication-related data operations. It wraps Firebase Authentication and Cloud Firestore tasks into a manageable interface.
PhoneAuthCallback (Interface)
Methods within the repository utilize this internal callback to communicate the state of asynchronous Firebase operations back to the caller.
| Method | Description | Parameters |
| :--- | :--- | :--- |
| onCodeSent | Triggered when the SMS verification code is successfully sent. | verificationId (String) |
| onSuccess | Triggered when the authentication process completes successfully. | userId (String) |
| onFailure | Triggered when an error occurs during the process. | error (String) |
Public Methods
startPhoneVerification
Initiates the Firebase Phone Authentication flow by requesting an OTP (One-Time Password) for the provided phone number.
public void startPhoneVerification(String phoneNumber, PhoneAuthCallback callback)
- Inputs:
phoneNumber: The user's phone number in E.164 format (e.g., +1234567890).callback: An instance ofPhoneAuthCallbackto handle the response.
verifyPhoneCode
Verifies the OTP entered by the user. If valid, it completes the authentication and persists the user's profile details to Firestore.
public void verifyPhoneCode(
String verificationId,
String code,
String userId,
String fullName,
String phoneNumber,
PhoneAuthCallback callback
)
- Inputs:
verificationId: The ID received from theonCodeSentcallback.code: The 6-digit OTP entered by the user.userId: The unique Firebase UID for the user.fullName: The user's full name to be stored in the profile.phoneNumber: The verified phone number.callback: An instance ofPhoneAuthCallbackto handle the verification result.
Usage Example
The following example demonstrates how a ViewModel interacts with the AuthRepository to initiate phone verification:
AuthRepository authRepository = new AuthRepository();
authRepository.startPhoneVerification("+919876543210", new AuthRepository.PhoneAuthCallback() {
@Override
public void onCodeSent(String verificationId) {
// Update UI to show OTP entry field
}
@Override
public void onSuccess(String userId) {
// Handle immediate success (e.g., instant verification)
}
@Override
public void onFailure(String error) {
// Display error message to the user
}
});
Data Integrity
While the Repository handles the communication with Firebase, it relies on the calling layer to provide pre-validated data. It ensures that upon successful phone verification, user documents are updated in the Firestore users collection with the detailsSubmitted flag to maintain a consistent application state.