deleteAccount method

Future<void> deleteAccount()

Permanently deletes the current user's account.

The server-side deletion runs before local sign-out so it still has an authenticated token. Sign-out is best-effort after the identity is gone.

Implementation

Future<void> deleteAccount() async {
  state = const AsyncLoading();

  state = await AsyncValue.guard(() async {
    final userId = ref.read(currentUserProvider)?.id;
    await ref.read(accountRepositoryProvider).deleteAccount();
    // Unlike a sign-out, this is the end of the account, so everything the
    // device holds for it goes, the games it played here included.
    if (userId != null) await deleteAccountData(ref, userId);
    try {
      await ref.read(authServiceProvider).signOut();
    } catch (error) {
      developer.log(
        'Sign-out after account deletion failed (ignored)',
        name: 'auth.controller',
        error: error,
      );
    }
  });
}