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!
- User is authenticated (
[email protected], ID:68fbe3ac4740d821e041f9df) - BUT app initializes as guest (
temp-9e793dc8-a24d-4343-aeea-6066f7969cd3) - Universe created in guest space (
temp-xxb7jayp) - On navigation, SpaceGuard detects actual authentication
- Guard CLEARS guest data to switch to authenticated space
- 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:
- In
main.ts: Check auth status BEFORE initializing SpaceService - If authenticated: Use real user space
- If guest: Use guest mode
- 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