delete method

Future<AuthResponse<T>> delete({
  1. Object? args,
  2. String? id,
  3. bool notifiable = true,
})
inherited

Implementation

Future<AuthResponse<T>> delete({
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  emit(
    const AuthResponse.loading(AuthType.delete),
    args: args,
    id: id,
    notifiable: notifiable,
  );

  final opToken = _beginOp();

  final data = await auth;
  if (data == null) {
    return emit(
      AuthResponse.failure(
        msg.loggedIn.failure ?? 'Not logged in',
        type: AuthType.delete,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  }

  try {
    final response = await delegate.delete();
    if (!_isOpAlive(opToken)) {
      return AuthResponse.failure('Cancelled', type: AuthType.delete);
    }
    if (!response.isSuccessful) {
      return _failure(
        response.error.isNotEmpty
            ? response.error
            : (msg.delete.failure ?? 'Delete failed'),
        type: AuthType.delete,
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

    try {
      await _backup.onDeleteUser(data.id);
    } catch (_) {}

    try {
      await delegate.signOut();
    } catch (_) {}

    final prev = _backupEmitEnabled;
    _backupEmitEnabled = false;
    try {
      await _clearLocal();
    } finally {
      _backupEmitEnabled = prev;
    }

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

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