CRITICAL AUTH TRANSITION BUG

Documentation for Plans

๐Ÿšจ CRITICAL: Guest/Auth User Transition Bug

Issue: Universe disappears due to guest-to-authenticated user space switching
Severity: P0 - DATA LOSS BUG
Status: ROOT CAUSE IDENTIFIED


๐Ÿ” Log Analysis - The Smoking Gun

What the Logs Show

Step 1: User Creates Universe (as Guest)

[SpaceService Init] Using guest user: 
  userId: 'temp-9e793dc8-a24d-4343-aeea-6066f7969cd3'
  spaceSlug: 'temp-xxb7jayp'

๐Ÿš€ Creating universe: new-pakos-uni-1
โœ… Universe saved to local storage
โœ… Space synced to backend

Step 2: Navigation Triggers Auth Check

[SpaceGuard] Auth check: 
  isAuthenticated: true, 
  userId: '68fbe3ac4740d821e041f9df' โ† DIFFERENT USER!

[SpaceGuard] Clearing guest space before initializing authenticated user space
๐Ÿงน [SpaceMapFlashState] Clearing flash state for user/space switch...
โœ… [SpaceMapFlashState] Flash state cleared โ† UNIVERSE DELETED!

Step 3: Load Authenticated User Space

[SpaceGuard] Initializing space for authenticated user: 
  userId: '68fbe3ac4740d821e041f9df'
  spaceSlug: 'chrome-space-1'

[SpaceService] Loaded universes: 0 โ† EMPTY! Universe is gone!

๐Ÿ”ด The Problem

User is logged in as [email protected] but operating in GUEST mode!

  1. User is authenticated ([email protected], ID: 68fbe3ac4740d821e041f9df)
  2. BUT app initializes as guest (temp-9e793dc8-a24d-4343-aeea-6066f7969cd3)
  3. Universe created in guest space (temp-xxb7jayp)
  4. On navigation, SpaceGuard detects actual authentication
  5. Guard CLEARS guest data to switch to authenticated space
  6. Universe lost!

This is a FUNDAMENTAL auth flow issue, not a persistence issue.


โœ… Solution Options

Option 1: Fix Initial Auth Detection (BEST)

Prevent guest mode when user is already authenticated:

// In main.ts SpaceService initialization
const user = authService.user();
if (user && user.email !== '[email protected]') {
  // User is authenticated - use their space
  await spaceService.initializeSpace(user.id, undefined);
} else {
  // Actually a guest - use guest mode
  await spaceService.initializeSpace(guestUser.userId, guestUser.spaceSlug);
}

Option 2: Migrate Guest Data on Auth

Transfer guest universe to authenticated user:

// In SpaceGuard when detecting auth after guest
if (wasGuestUser && nowAuthenticated) {
  await migrateGuestDataToAuthUser();
  // Don't clear, migrate instead!
}

Option 3: Don't Use Guest Mode for Authenticated Users

Simplest: If user is logged in, always use their authenticated space from start


๐ŸŽฏ Recommended Fix

Option 1 + 3 Combined:

  1. In main.ts: Check auth status BEFORE initializing SpaceService
  2. If authenticated: Use real user space
  3. If guest: Use guest mode
  4. Never switch: Once in authenticated mode, stay there

File to Modify: apps/gograb-space/src/main.ts around line 330-360


๐Ÿ“ Current Bad Flow

App Init
  โ†“
Check auth โ†’ User IS authenticated ([email protected])
  โ†“
BUT code initializes as guest anyway! โŒ
  โ†“
Guest user: temp-9e793dc8-a24d-4343-aeea-6066f7969cd3
  โ†“
Create universe in guest space
  โ†“
Navigate โ†’ SpaceGuard runs
  โ†“
Guard: "Wait, user IS authenticated!"
  โ†“
Clear guest data โŒ
  โ†“
Initialize authenticated space
  โ†“
Universe gone!

โœ… Correct Flow Should Be

App Init
  โ†“
Check auth โ†’ User IS authenticated ([email protected])
  โ†“
Initialize with authenticated user space โœ…
  โ†“
userId: 68fbe3ac4740d821e041f9df
spaceSlug: chrome-space-1
  โ†“
Create universe in AUTHENTICATED space
  โ†“
Navigate โ†’ SpaceGuard runs
  โ†“
Guard: "User is authenticated, space already correct"
  โ†“
No clearing needed โœ…
  โ†“
Universe persists!

๐Ÿ”ง Fix Location

File: apps/gograb-space/src/main.ts
Lines: ~330-360 (SpaceService initialization)

Current Code:

// Line 340
๐ŸŽญ [SpaceService Init] Using guest user

Should Be:

const user = authService.user();
if (user && user.id) {
  // User is authenticated - use their space
  await spaceService.initializeSpace(user.id, undefined);
} else {
  // Actually a guest - use guest mode
  const guestUser = guestAuthService.getOrCreateGuestUser();
  await spaceService.initializeSpace(guestUser.userId, guestUser.spaceSlug);
}

๐Ÿšจ Impact

Critical: Every authenticated user who creates a universe will lose it on navigation!

Must Fix: Before any real testing


Priority: P0 - IMMEDIATE
Complexity: MEDIUM - Auth flow refactor needed
Time: 30-60 minutes