Skip to content

fix: proper auth race condition fix (replaces #75) - #82

Merged
IkeyBenz merged 5 commits into
mainfrom
fix/revert-and-fix-auth-race
Feb 8, 2026
Merged

IkeyBenz merged 5 commits into
mainfrom
fix/revert-and-fix-auth-race

Conversation

@IkeyBenz

@IkeyBenz IkeyBenz commented Feb 8, 2026

Copy link
Copy Markdown
Collaborator

What PR #75 was trying to fix

PR #75 (e3a06b2) fixed a blank green screen that appeared when opening a game link directly (e.g. /games/1234). The root cause was that the splash screen hid before auth completed.

Why PR #75's approach introduced new race conditions

PR #75 changed AuthProvider to use onAuthStateChanged and set isLoading=false immediately when the user was detected, refreshing the token in the background (fire-and-forget). This meant:

  • userId was set and isLoading was false BEFORE the token refresh completed
  • Downstream code like useCreateGame checks isLoading and userId before calling getIdToken()
  • Since both were ready before the token was actually refreshed, API calls could use stale/invalid tokens

What this fix does differently

This fix keeps onAuthStateChanged (which is faster than signInAnonymously for returning users since Firebase caches auth state) but awaits the token refresh before setting isLoading=false:

auth.onAuthStateChanged(async (user) => {
  if (user) {
    setUserId(user.uid);
    await user.getIdToken(true); // Wait for token refresh
    setIsLoading(false);         // NOW auth is truly ready
  }
});

This ensures:

  • ✅ Splash screen stays visible until auth + token are BOTH ready
  • ✅ No green screen on direct game links
  • ✅ No token race conditions (getIdToken always returns valid token)
  • ✅ Fast auth for returning users via cached onAuthStateChanged

How it was tested

  1. Game creation: Created a game with username, navigated to lobby — no token errors
  2. Direct link: Navigated directly to /games/{PIN} — game loaded correctly, no green screen
  3. Verified splash screen stays visible during auth loading

…ing auth ready

PR #75 fixed the green screen bug on direct game links by using
onAuthStateChanged, but introduced a new race condition: it set
isLoading=false before the token refresh completed, so downstream
code (like useCreateGame) could call getIdToken() and get stale tokens.

This fix keeps onAuthStateChanged for fast auth pickup on returning
users, but crucially awaits the token refresh BEFORE setting
isLoading=false. This ensures:

- Splash screen stays visible until auth is FULLY ready (userId + token)
- No green screen on direct game links
- No token race conditions for downstream API calls

Replaces the approach from PR #75 (commit e3a06b2).
@vercel

vercel Bot commented Feb 8, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
modi-app Ready Ready Preview, Comment Feb 8, 2026 6:31pm

The GameScreen was rendering an empty green ScreenContainer when
auth or game data wasn't ready yet. On mobile Safari, the splash
screen hides before the game screen has its data, leaving users
staring at a blank green screen.

Now properly distinguishes between:
- Loading (auth pending or game data loading) → shows spinner
- Game not found (null) → empty screen (useGame redirects home)
- Ready → renders the game

This is the actual fix for the mobile Safari green screen issue.
Root cause: useGame() subscribed to Firestore immediately on mount,
before Firebase Auth completed. On mobile Safari (where auth is slower
than desktop Chrome), this caused either permission errors or empty
snapshots, which triggered a premature redirect to '/' — resulting in
a blank green screen.

Fix:
- useGame: skip Firestore subscription until auth is ready (isLoading=false && userId set)
- GameScreen: show spinner instead of empty green screen when game is null (redirecting)
@IkeyBenz
IkeyBenz temporarily deployed to modi-api-pr-82 February 8, 2026 18:28 Inactive
@IkeyBenz
IkeyBenz merged commit 7108374 into main Feb 8, 2026
1 of 2 checks passed

This branch was successfully deployed

1 active and 1 inactive deployments
Preview — dfdaf690 Deployed Feb 8, 2026 by vercel[bot]
modi-api-pr-82 — dfdaf690 Deployed Feb 8, 2026 by IkeyBenz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant