upgradeToGoogle method

Future<UpgradeOutcome> upgradeToGoogle()

Upgrades the current guest session to a permanent Google account.

A successful link preserves the user id, games, ratings, and friends. If the Google identity already belongs to an account, no session is changed; the UI must obtain confirmation before calling switchToExisting.

Implementation

Future<UpgradeOutcome> upgradeToGoogle() async {
  state = const AsyncLoading();

  final authService = ref.read(authServiceProvider);
  final analytics = ref.read(analyticsServiceProvider);
  final guestId = ref.read(currentUserProvider)?.id;
  try {
    final result = await authService.upgradeWithGoogle();
    if (result == AuthUpgradeResult.existingAccount) {
      _hasPendingExistingAccount = true;
      state = const AsyncData(null);
      return UpgradeOutcome.existingAccount;
    }
    if (guestId != null) {
      ref.invalidate(currentUserProfileProvider);
      ref.invalidate(playerInfoCacheProvider(id: guestId));
    }
    unawaited(analytics.guestUpgraded());
    unawaited(analytics.setAccountType(isGuest: false));
    state = const AsyncData(null);
    return UpgradeOutcome.linked;
  } catch (error, stackTrace) {
    state = AsyncError(error, stackTrace);
    rethrow;
  }
}