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);
  try {
    final result = await authService.upgradeWithGoogle();
    if (result == AuthUpgradeResult.cancelled) {
      state = const AsyncData(null);
      return UpgradeOutcome.cancelled;
    }
    if (result == AuthUpgradeResult.existingAccount) {
      _hasPendingExistingAccount = true;
      state = const AsyncData(null);
      return UpgradeOutcome.existingAccount;
    }
    // Linking replaced the guest's name and avatar with the provider's, and
    // the next sync states them.
    unawaited(ref.read(syncCoordinatorProvider.notifier).run());
    unawaited(analytics.guestUpgraded());
    unawaited(analytics.setAccountType(isGuest: false));
    state = const AsyncData(null);
    return UpgradeOutcome.linked;
  } catch (error, stackTrace) {
    state = AsyncError(error, stackTrace);
    rethrow;
  }
}