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,
})
inherited

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.resendToken ?? ''),
      multiFactorInfo: multiFactorInfo,
      multiFactorSession: multiFactorSession,
      timeout: timeout,
      onComplete: (credential) async {
        if (onComplete != null) {
          emit(
            const AuthResponse.message(
              'Verification done!',
              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) {
            await signInByOtp(
              OtpAuthenticator.phone(
                token: verId,
                code: code,
                phone: authenticator.phone,
              ),
              args: args,
              id: id,
              notifiable: notifiable,
            );
          } else {
            emit(
              const AuthResponse.failure(
                'Verification token or otp code not valid!',
                type: AuthType.otp,
              ),
              args: args,
              id: id,
              notifiable: notifiable,
            );
          }
        }
      },
      onCodeSent: (verId, forceResendingToken) {
        emit(
          const AuthResponse.message(
            'Code sent to your device!',
            type: AuthType.otp,
          ),
          args: args,
          id: id,
          notifiable: notifiable,
        );
        onCodeSent?.call(verId, forceResendingToken);
      },
      onFailed: (exception) {
        emit(
          AuthResponse.failure(
            exception.msg,
            type: AuthType.otp,
          ),
          args: args,
          id: id,
          notifiable: notifiable,
        );
        onFailed?.call(exception);
      },
      onCodeAutoRetrievalTimeout: (verId) {
        emit(
          const AuthResponse.failure(
            'Auto retrieval code timeout!',
            type: AuthType.otp,
          ),
          args: args,
          id: id,
          notifiable: notifiable,
        );
        onCodeAutoRetrievalTimeout?.call(verId);
      },
    );
    return emit(
      const AuthResponse.loading(AuthType.otp),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  } catch (error) {
    return _failure(
      msg.signOut.failure ?? error.toString(),
      type: AuthType.otp,
      args: args,
      id: id,
      notifiable: notifiable,
    );
  }
}