signInByOtp method

Future<AuthResponse<T>> signInByOtp(
  1. OtpAuthenticator authenticator, {
  2. bool storeToken = false,
  3. Object? args,
  4. String? id,
  5. bool notifiable = true,
})
inherited

Implementation

Future<AuthResponse<T>> signInByOtp(
  OtpAuthenticator authenticator, {
  bool storeToken = false,
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  emit(
    const AuthResponse.loading(AuthType.phone),
    args: args,
    id: id,
    notifiable: notifiable,
  );

  final opToken = _beginOp();

  try {
    final hasAnonymous = this.hasAnonymous;

    final response = await delegate.signInWithOtp(
      authenticator.token,
      authenticator.code,
    );

    if (!_isOpAlive(opToken)) {
      return AuthResponse.failure('Cancelled', type: AuthType.phone);
    }

    if (!response.isSuccessful) {
      return _failure(
        response.error,
        type: AuthType.phone,
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

    final result = response.data;
    final uid = result?.uid ?? '';
    if (result == null || uid.isEmpty) {
      return _failure(
        msg.authorization,
        type: AuthType.phone,
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

    final value = await _update(
      id: uid,
      hasAnonymous: hasAnonymous,
      data: {
        keys.id: uid,
        keys.loggedIn: true,
        keys.loggedInTime: EntityHelper.generateTimeMills,
        keys.provider: 'PHONE_NUMBER',
        keys.phone: authenticator.value,
        keys.verified: true,
      },
    );

    if (!_isOpAlive(opToken)) {
      return AuthResponse.failure('Cancelled', type: AuthType.phone);
    }

    if (value == null) {
      return _failure(
        msg.authorization,
        type: AuthType.phone,
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

    return emit(
      AuthResponse.authenticated(
        value,
        msg: msg.signInWithPhone.done,
        type: AuthType.phone,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  } catch (error) {
    return _failure(
      msg.signInWithPhone.failure ?? error.toString(),
      type: AuthType.phone,
      args: args,
      id: id,
      notifiable: notifiable,
    );
  }
}