authenticateWithOtp method

Future<FirebaseAuthenticationResult> authenticateWithOtp(
  1. String otp
)

Authenticate the user using SMS code otp

Native Platform support

Implementation

Future<FirebaseAuthenticationResult> authenticateWithOtp(String otp) async {
  if (_mobileVerificationId == null) {
    throw 'The _mobileVerificationId should not be null here. Verification was probably skipped.';
  }

  try {
    final phoneAuthCredential = PhoneAuthProvider.credential(
      verificationId: _mobileVerificationId!,
      smsCode: otp,
    );

    final userCredential = await firebaseAuth.signInWithCredential(
      phoneAuthCredential,
    );

    return FirebaseAuthenticationResult(user: userCredential.user);
  } on FirebaseAuthException catch (e) {
    log?.e('A Firebase exception has occurred. $e');
    return FirebaseAuthenticationResult.error(
      exceptionCode: e.code.toLowerCase(),
      errorMessage: getErrorMessageFromFirebaseException(e),
    );
  } on Exception catch (e) {
    log?.e('A general exception has occurred. $e');
    return FirebaseAuthenticationResult.error(
      errorMessage:
          'We could not authenticate with OTP at this time. Please try again.',
    );
  }
}