verifyOtp method

Future<bool> verifyOtp(
  1. String otp
)

Verify the OTP sent to _phoneNumber and login user is OTP was correct.

Returns true if the otp passed was correct and the user was logged in successfully. On login success, _onLoginSuccess is called.

If the otp passed is incorrect, or the otp is expired or any other error occurs, the functions returns false.

Also, _onLoginFailed is called with FirebaseAuthException object to handle the error.

Implementation

Future<bool> verifyOtp(String otp) async {
  if ((!kIsWeb && _verificationId == null) ||
      (kIsWeb && _webConfirmationResult == null)) return false;

  try {
    if (kIsWeb) {
      final userCredential = await _webConfirmationResult!.confirm(otp);
      return await _loginUser(
        userCredential: userCredential,
        autoVerified: false,
      );
    } else {
      final credential = PhoneAuthProvider.credential(
        verificationId: _verificationId!,
        smsCode: otp,
      );
      return await _loginUser(
        authCredential: credential,
        autoVerified: false,
      );
    }
  } on FirebaseAuthException catch (e, s) {
    _onLoginFailed?.call(e, s);
    return false;
  } catch (e, s) {
    _onError?.call(e, s);
    return false;
  }
}