signInByPhone method

Future<AuthResponse<T>> signInByPhone(
  1. PhoneAuthenticator authenticator, {
  2. Object? multiFactorInfo,
  3. Object? multiFactorSession,
  4. Duration timeout = const Duration(minutes: 2),
  5. void onComplete(
    1. Credential credential
    )?,
  6. void onFailed(
    1. AuthException exception
    )?,
  7. void onCodeSent(
    1. String verId,
    2. int? forceResendingToken
    )?,
  8. void onCodeAutoRetrievalTimeout(
    1. String verId
    )?,
  9. Object? args,
  10. String? id,
  11. bool notifiable = true,
})

Implementation

Future<AuthResponse<T>> signInByPhone(
  PhoneAuthenticator authenticator, {
  Object? multiFactorInfo,
  Object? multiFactorSession,
  Duration timeout = const Duration(minutes: 2),
  void Function(Credential credential)? onComplete,
  void Function(AuthException exception)? onFailed,
  void Function(String verId, int? forceResendingToken)? onCodeSent,
  void Function(String verId)? onCodeAutoRetrievalTimeout,
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  try {
    delegate.verifyPhoneNumber(
      phoneNumber: authenticator.phone,
      forceResendingToken: int.tryParse(authenticator.accessToken ?? ""),
      multiFactorInfo: multiFactorInfo,
      multiFactorSession: multiFactorSession,
      timeout: timeout,
      onComplete: (credential) async {
        if (onComplete != null) {
          emit(
            const AuthResponse.message(
              "Verification done!",
              provider: Provider.phone,
              type: AuthType.otp,
            ),
            args: args,
            id: id,
            notifiable: notifiable,
          );
          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: Provider.phone,
                type: AuthType.otp,
              ),
              args: args,
              id: id,
              notifiable: notifiable,
            );
          }
        }
      },
      onCodeSent: (String verId, int? forceResendingToken) {
        emit(
          const AuthResponse.message(
            "Code sent to your device!",
            provider: Provider.phone,
            type: AuthType.otp,
          ),
          args: args,
          id: id,
          notifiable: notifiable,
        );
        if (onCodeSent != null) onCodeSent(verId, forceResendingToken);
      },
      onFailed: (exception) {
        emit(
          AuthResponse.failure(
            exception.msg,
            provider: Provider.phone,
            type: AuthType.otp,
          ),
          args: args,
          id: id,
          notifiable: notifiable,
        );
        if (onFailed != null) onFailed(exception);
      },
      onCodeAutoRetrievalTimeout: (String verId) {
        emit(
          const AuthResponse.failure(
            "Auto retrieval code timeout!",
            provider: Provider.phone,
            type: AuthType.otp,
          ),
          args: args,
          id: id,
          notifiable: notifiable,
        );
        if (onCodeAutoRetrievalTimeout != null) {
          onCodeAutoRetrievalTimeout(verId);
        }
      },
    );
    return emit(
      const AuthResponse.loading(
        Provider.phone,
        AuthType.otp,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  } catch (error) {
    return emit(
      AuthResponse.failure(
        msg.signOut.failure ?? error,
        provider: Provider.phone,
        type: AuthType.otp,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  }
}