registerWithEmailAndPassword method

  1. @override
Future<Either<AuthFailure, Unit>> registerWithEmailAndPassword({
  1. required String email,
  2. required String password,
})
override

Register with Email and password return Future<Either<AuthFailure, Unit>>

Implementation

@override
Future<Either<AuthFailure, Unit>> registerWithEmailAndPassword(
    {required String email, required String password}) async {
  try {
    final previousUser = getSignedInUser();
    if (previousUser != null) {
      final authCreds =
          EmailAuthProvider.credential(email: email, password: password);
      await previousUser.linkWithCredential(authCreds);
    } else {
      await _firebaseAuth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
    }
    return const Right(unit);
  } on FirebaseAuthException catch (e) {
    if (e.code == kFirebaseCodeEmailAlreadyInUse) {
      return const Left(AuthFailure.emailAlreadyInUse());
    } else if (e.code == kFirebaseCodeInvalidEmail) {
      return const Left(AuthFailure.invalidEmail());
    } else if (e.code == kFirebaseCodeWeakPassword) {
      return const Left(AuthFailure.weakPassword());
    } else if (e.code == kFirebaseCodeOperationNotAllowed) {
      return const Left(AuthFailure.operationNotAllowed());
    } else if (e.code == kFirebaseCodeInvalidCredentials) {
      return const Left(AuthFailure.invalidCredentials());
    } else {
      return const Left(AuthFailure.serverError());
    }
  }
}