signInWithEmailAndPassword method

Future<UserCredential?> signInWithEmailAndPassword({
  1. required String email,
  2. required String password,
  3. bool updateProfile = false,
  4. String? displayName,
  5. String? photoURL,
  6. dynamic onError(
    1. String
    )?,
})

signInWithEmailAndPassword method to login to existing account

Following error will be added to _errorStreamController if conditions aren't met

  • Provided email is invalid:
  • Thrown if the email address is not valid.
  • Account has been banned:
  • Thrown if the user corresponding to the given email has been disabled.
  • Account does not exist:
  • Thrown if there is no user corresponding to the given email.
  • Provided password is wrongss:
  • Thrown if the password is invalid for the given email, or the account corresponding to the email does not have a password set.

Implementation

Future<UserCredential?> signInWithEmailAndPassword({
  required String email,
  required String password,
  bool updateProfile = false,
  String? displayName,
  String? photoURL,
  Function(String)? onError,
}) async {
  try {
    var creds = await _auth.signInWithEmailAndPassword(
        email: email, password: password);
    if (creds.user != null) _errorStreamController.sink.add(null);
    if (updateProfile) {
      await creds.user?.updateDisplayName(displayName);
      await creds.user?.updatePhotoURL(photoURL);
    }
    return creds;
  } on FirebaseAuthException catch (e) {
    switch (e.code) {
      case 'user-disabled':
        if (onError != null) onError('Account has been banned');
        _errorStreamController.sink.add('Account has been banned');
        break;
      case 'invalid-email':
        if (onError != null) onError('Provided email is invalid');
        _errorStreamController.sink.add('Provided email is invalid');
        break;
      case 'user-not-found':
        if (onError != null) onError('Account does not exist');
        _errorStreamController.sink.add('Account does not exist');
        break;
      case 'wrong-password':
        if (onError != null) onError('Provided password is wrong');
        _errorStreamController.sink.add('Provided password is wrong');
        break;
      default:
        if (onError != null) onError('Error occured. Contact admin!!');
        _errorStreamController.add('Error occured. Contact admin!!');
        break;
    }
  }
}