unregisterDevice method

  1. @override
Future<Either<RepositoryFailure, Unit>> unregisterDevice()
override

Removes the current device registration from Firestore.

Called BEFORE logout while still authenticated, via the addOnAboutToLogOutCallback hook. Deletes the device document at users/{uid}/devices/{deviceId}.

Important

  • Must be called while still authenticated (before sign out)
  • Best-effort and timeboxed; must never block logout
  • If offline, staleness cleanup will handle orphaned docs

When Called

  • Automatically via AuthServiceInt.addOnAboutToLogOutCallback
  • Should not be called manually in most cases

Returns

  • Right(unit) on success
  • Left(RepositoryFailure) on failure (logged, doesn't block logout)

Implementation

@override
Future<Either<RepositoryFailure, Unit>> unregisterDevice() async {
  logd('DeviceService: unregisterDevice called');

  try {
    final deviceId = await getDeviceId();

    logd('DeviceService: Unregistering device $deviceId');

    final result = await _deviceCallable.call({
      'action': 'unregister',
      'deviceId': deviceId,
    });

    final data = Map<String, dynamic>.from(result.data as Map);
    if (data['success'] != true) {
      logw('DeviceService: Unregister response indicated failure');
      return const Left(RepositoryFailure.unexpected);
    }

    logd('DeviceService: Device unregistered successfully');
    return const Right(unit);
  } on FirebaseFunctionsException catch (e) {
    loge(e, 'DeviceService: Firebase Functions error during unregistration');
    return _mapFirebaseFunctionsException(e);
  } catch (e) {
    loge(e, 'DeviceService: Unexpected error during unregistration');
    return const Left(RepositoryFailure.unexpected);
  }
}