loginWithPhoneVerifyCode method

  1. @override
Future<Either<PhoneAuthError, bool>> loginWithPhoneVerifyCode(
  1. String smsCode
)
override

Implementation

@override
Future<Either<PhoneAuthError, bool>> loginWithPhoneVerifyCode(String smsCode) async {
  try {
    // Create a PhoneAuthCredential with the verification ID and the SMS code
    PhoneAuthCredential credential = PhoneAuthProvider.credential(
      verificationId: _phoneVerificationId,
      smsCode: smsCode,
    );

    // Sign in the user with the credential
    await _fbAuth.signInWithCredential(credential);

    //TODO: handle the Firebase Errors to see if the code was wrong
    return right(true);
  } on FirebaseAuthException catch (e) {
    if (e.code == 'invalid-verification-code') {
      // print('The provided verification code is invalid.');
      return left(PhoneAuthError.wrongSmsCode);
      // Handle invalid verification code
    } else if (e.code == 'user-not-found') {
      // print('No user found with this phone number.');
      return left(PhoneAuthError.invalidPhone);
      // Handle no user found
    } else {
      logd(
          'e.code was something unhandled in loginWithPhoneVerifyCode:${e.message ?? 'no message'}');
      // Handle other Firebase auth errors
      return left(PhoneAuthError.unexpected);
    }
  } catch (e) {
    loge(e);
    return left(PhoneAuthError.unexpected);
  }
}