signInWithEmailAndPassword method

Future<UserCredential> signInWithEmailAndPassword(
  1. String email,
  2. String password
)

Signs in an existing user with email and password

Implementation

Future<UserCredential> signInWithEmailAndPassword(
  String email,
  String password
) async {
  try {
    // Pre-condition checks
    if (email.isEmpty || password.isEmpty) {
      throw ValidationError('Email and password are required');
    }

    return await _auth.signInWithEmailAndPassword(
      email: email,
      password: password,
    );
  } on FirebaseAuthException catch (e) {
    // Handle specific Firebase Auth errors
    switch (e.code) {
      case 'user-not-found':
        throw ValidationError('No user found with this email', cause: e);
      case 'wrong-password':
        throw ValidationError('Incorrect password', cause: e);
      case 'invalid-email':
        throw ValidationError('The email address is not valid', cause: e);
      case 'user-disabled':
        throw StateError('This account has been disabled', cause: e);
      case 'too-many-requests':
        throw NetworkException('Too many sign-in attempts. Please try again later', cause: e);
      default:
        throw RALIException('Failed to sign in: ${e.message}', cause: e);
    }
  } catch (e) {
    // Handle other error types
    if (e is RALIException) {
      // Already a RALIException, just rethrow
      rethrow;
    }
    // Log the error for debugging
    logError(e, StackTrace.current, context: 'signInWithEmailAndPassword');
    throw RALIException('An unexpected error occurred during sign in', cause: e);
  }
}