Fix race condition causing permissions error on /display page

onAuthStateChanged fires with null before signInAnonymously completes,
causing DisplayView to query Firestore unauthenticated. Now only marks
auth ready when an actual user is present; auth failures are handled in
the catch block to avoid hanging the UI.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-16 10:53:03 -04:00
parent 4150267925
commit 33d831af54
+8
View File
@@ -2658,13 +2658,21 @@ function App() {
} catch (err) { } catch (err) {
console.error("Authentication error:", err); console.error("Authentication error:", err);
setError("Failed to authenticate. Please try again later."); setError("Failed to authenticate. Please try again later.");
// Auth failed and onAuthStateChanged won't fire with a user, so unblock the UI here
setIsAuthReady(true);
setIsLoading(false);
} }
}; };
const unsubscribe = onAuthStateChanged(auth, (user) => { const unsubscribe = onAuthStateChanged(auth, (user) => {
setUserId(user ? user.uid : null); setUserId(user ? user.uid : null);
// Only mark auth ready once we have an actual authenticated user.
// onAuthStateChanged fires with null before signInAnonymously completes,
// which would cause Firestore queries to run unauthenticated.
if (user) {
setIsAuthReady(true); setIsAuthReady(true);
setIsLoading(false); setIsLoading(false);
}
}); });
initAuth(); initAuth();