signInByPhone method

  1. @override
Future<AuthResponse<T>> signInByPhone(
  1. PhoneAuthenticator authenticator, {
  2. PhoneMultiFactorInfo? multiFactorInfo,
  3. MultiFactorSession? multiFactorSession,
  4. Duration timeout = const Duration(minutes: 2),
  5. void onComplete(
    1. PhoneAuthCredential credential
    )?,
  6. void onFailed(
    1. FirebaseAuthException exception
    )?,
  7. void onCodeSent(
    1. String verId,
    2. int? forceResendingToken
    )?,
  8. void onCodeAutoRetrievalTimeout(
    1. String verId
    )?,
})
override

Implementation

@override
Future<AuthResponse<T>> signInByPhone(
  PhoneAuthenticator authenticator, {
  PhoneMultiFactorInfo? multiFactorInfo,
  MultiFactorSession? multiFactorSession,
  Duration timeout = const Duration(minutes: 2),
  void Function(PhoneAuthCredential credential)? onComplete,
  void Function(FirebaseAuthException exception)? onFailed,
  void Function(String verId, int? forceResendingToken)? onCodeSent,
  void Function(String verId)? onCodeAutoRetrievalTimeout,
}) async {
  final phone = authenticator.phone;
  if (!AuthValidator.isValidPhone(phone)) {
    return emit(AuthResponse.failure(
      msg.phoneNumber,
      provider: AuthProviders.phone,
      type: AuthType.otp,
    ));
  } else {
    try {
      authHandler.signInByPhone(
        phoneNumber: phone,
        forceResendingToken: int.tryParse(authenticator.accessToken ?? ""),
        multiFactorInfo: multiFactorInfo,
        multiFactorSession: multiFactorSession,
        timeout: timeout,
        onComplete: (PhoneAuthCredential credential) async {
          if (onComplete != null) {
            emit(const AuthResponse.message(
              "Verification done!",
              provider: AuthProviders.phone,
              type: AuthType.otp,
            ));
            onComplete(credential);
          } else {
            final verId = credential.verificationId;
            final code = credential.smsCode;
            if (verId != null && code != null) {
              signInByOtp(authenticator.otp(
                token: verId,
                smsCode: code,
              ));
            } else {
              emit(const AuthResponse.failure(
                "Verification token or otp code not valid!",
                provider: AuthProviders.phone,
                type: AuthType.otp,
              ));
            }
          }
        },
        onCodeSent: (String verId, int? forceResendingToken) {
          emit(const AuthResponse.message(
            "Code sent to your device!",
            provider: AuthProviders.phone,
            type: AuthType.otp,
          ));
          if (onCodeSent != null) {
            onCodeSent(verId, forceResendingToken);
          }
        },
        onFailed: (FirebaseAuthException exception) {
          emit(AuthResponse.failure(
            exception.message,
            provider: AuthProviders.phone,
            type: AuthType.otp,
          ));
          if (onFailed != null) {
            onFailed(exception);
          }
        },
        onCodeAutoRetrievalTimeout: (String verId) {
          emit(const AuthResponse.failure(
            "Auto retrieval code timeout!",
            provider: AuthProviders.phone,
            type: AuthType.otp,
          ));
          if (onCodeAutoRetrievalTimeout != null) {
            onCodeAutoRetrievalTimeout(verId);
          }
        },
      );
      return emit(const AuthResponse.loading(
        AuthProviders.phone,
        AuthType.otp,
      ));
    } catch (_) {
      return emit(AuthResponse.failure(
        msg.signOut.failure ?? _,
        provider: AuthProviders.phone,
        type: AuthType.otp,
      ));
    }
  }
}