Skip to content

Consolidate Firebase initialization and fix auth context conflicts#15

Open
DigitalBlueprint239 wants to merge 1 commit into
masterfrom
claude/auth-hardening-firestore-consolidation-E8Wrh
Open

Consolidate Firebase initialization and fix auth context conflicts#15
DigitalBlueprint239 wants to merge 1 commit into
masterfrom
claude/auth-hardening-firestore-consolidation-E8Wrh

Conversation

@DigitalBlueprint239

Copy link
Copy Markdown
Owner

Summary

This PR consolidates Firebase initialization into a single shared instance and fixes critical auth context conflicts that were preventing proper authentication. The app now uses email/password authentication with proper error handling, and all Firebase operations flow through a single initialization point.

Key Changes

Firebase Initialization & Consolidation

  • src/services/firebase.ts: Converted to single source of truth for Firebase initialization using getApps() guard to prevent multiple app instances
  • src/services/firestore.ts: Removed duplicate initializeApp() call; now imports db and auth from shared firebase.ts
  • src/components/SmartPlaybook/src/services/firebase.ts: Consolidated to re-export from shared firebase.ts instead of initializing independently
  • Fixed environment variable prefix from NEXT_PUBLIC_ to REACT_APP_ (Create React App requirement)

Authentication Architecture

  • src/hooks/useAuth.tsx: Implemented email/password authentication with browserLocalPersistence for session persistence across page refreshes
  • src/components/auth/LoginPage.tsx: New dedicated login/signup form with comprehensive Firebase error code translation to user-friendly messages
  • src/components/AuthProvider.tsx: Converted to re-export from hooks/useAuth.tsx, eliminating the previous anonymous auth context that was shadowing the real auth provider
  • src/App.tsx: Refactored to use state-based routing with proper auth gating:
    • Shows loading spinner during initial auth state resolution (prevents flash of login screen)
    • Unauthenticated users see LoginPage only
    • Authenticated users see Dashboard with TeamProvider and AIProvider mounted only after auth confirms

Supporting Changes

  • src/components/Dashboard.tsx: Wired usePracticePlans hook to display real Firestore practice plan count via real-time subscription
  • .env.local.example: Updated with REACT_APP_ prefix and added documentation explaining Create React App environment variable requirements
  • MASTER_TRACKER.md: Added comprehensive project state documentation including file index, auth architecture, and issue resolution history

Implementation Details

Why the consolidation matters:

  • Previously, multiple Firebase initializeApp() calls could create separate app instances, causing data to be written to different databases or auth conflicts
  • The SmartPlaybook sub-directory had its own Firebase initialization that was never used by the active app, creating a silent failure point

Auth flow:

  • Firebase persistence is set to browserLocalPersistence so users stay logged in across page refreshes
  • onAuthStateChanged listener in useAuth.tsx resolves the persisted session before rendering any protected content
  • Error codes from Firebase are translated to human-readable messages (e.g., "auth/wrong-password" → "Incorrect email or password")
  • No React Router used — routing is state-based (authenticated vs not authenticated)

Zero breaking changes:

  • All existing imports continue to work (AuthProvider re-export maintains backward compatibility)
  • Firestore service layer unchanged in public API
  • Dashboard and other components work with the new auth system without modification

https://claude.ai/code/session_01QCc5kbTa6iyNNFPpPUeiNk

Critical infrastructure fixes that were causing the app to crash on load
or silently break the authentication flow:

1. firebase.ts: Changed NEXT_PUBLIC_ to REACT_APP_ (CRA env prefix).
   Removed crash-on-missing-vars; now logs a warning instead so the UI
   renders the login screen rather than a blank white page.

2. firestore.ts: Removed independent Firebase app initialization with
   VITE_* env vars. Now imports db/auth from the shared firebase.ts.
   Prevents a second Firebase instance from being created silently.

3. AuthProvider.tsx: Replaced anonymous signInAnonymously provider with
   a re-export of hooks/useAuth.tsx. The two AuthContext instances were
   causing every Dashboard mount to throw "useAuth must be used within
   an AuthProvider" because App.tsx provided one context and Dashboard
   read from a completely separate one.

4. LoginPage.tsx: Created src/components/auth/LoginPage.tsx with
   email/password sign-in and sign-up, client-side validation, and
   Firebase error code → human-readable message translation.

5. App.tsx: Added AppContent auth gate. Unauthenticated users always
   see LoginPage. Auth loading state shows a spinner (no flash). Team
   and AI providers now mount only after auth resolves, so TeamContext
   always has a valid auth.currentUser on first render.

6. SmartPlaybook/src/services/firebase.ts: Consolidated orphaned second
   Firebase app to re-export from the shared instance.

7. Dashboard.tsx: Wired Practice Plans stat to real Firestore count via
   usePracticePlans hook (was hardcoded to 0).

8. .env.local.example: Corrected to REACT_APP_ prefix with explanation.

9. MASTER_TRACKER.md: Created living project tracker with file index,
   auth architecture diagram, resolved/deferred issue lists, and next
   session guidance (player roster UI).

https://claude.ai/code/session_01QCc5kbTa6iyNNFPpPUeiNk
@netlify

netlify Bot commented Feb 24, 2026

Copy link
Copy Markdown

Deploy Preview for magical-starlight-0c1207 failed.

Name Link
🔨 Latest commit f12a6ee
🔍 Latest deploy log https://app.netlify.com/projects/magical-starlight-0c1207/deploys/699de34aa9aeb0000802bdf7

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.

2 participants