Reapply "M2: gate storage init on STORAGE mode (firebase vs ws/memory)"

This reverts commit e843acdf8a.
This commit is contained in:
david raistrick
2026-06-29 12:36:16 -04:00
parent e843acdf8a
commit 17245dfa1b
+28 -9
View File
@@ -1,7 +1,7 @@
import React, { useState, useEffect, useRef, useMemo } from 'react'; import React, { useState, useEffect, useRef, useMemo } from 'react';
import { initializeApp } from './storage'; import { initializeApp } from './storage';
import { getAuth, signInAnonymously, onAuthStateChanged, signInWithCustomToken } from './storage'; import { getAuth, signInAnonymously, onAuthStateChanged, signInWithCustomToken } from './storage';
import { getFirestore, doc, setDoc, addDoc, collection, onSnapshot, updateDoc, deleteDoc, query, orderBy, limit, writeBatch, getStorage } from './storage'; import { getFirestore, doc, setDoc, addDoc, collection, onSnapshot, updateDoc, deleteDoc, query, orderBy, limit, writeBatch, getStorage, getStorageMode } from './storage';
import { import {
PlusCircle, Users, Swords, Trash2, Eye, Edit3, Save, XCircle, ChevronsUpDown, PlusCircle, Users, Swords, Trash2, Eye, Edit3, Save, XCircle, ChevronsUpDown,
UserCheck, UserX, HeartCrack, HeartPulse, Zap, EyeOff, ExternalLink, AlertTriangle, UserCheck, UserX, HeartCrack, HeartPulse, Zap, EyeOff, ExternalLink, AlertTriangle,
@@ -97,16 +97,18 @@ let db;
let auth; let auth;
let storage; let storage;
// Initialize Firebase const STORAGE_MODE = getStorageMode();
const initializeFirebase = () => {
// Initialize storage backend. firebase mode = real SDK init.
// ws/memory mode = mock auth, no firebase.
const initializeStorage = () => {
if (STORAGE_MODE === 'firebase') {
const requiredKeys = ['apiKey', 'authDomain', 'projectId', 'appId']; const requiredKeys = ['apiKey', 'authDomain', 'projectId', 'appId'];
const missingKeys = requiredKeys.filter(key => !firebaseConfig[key]); const missingKeys = requiredKeys.filter(key => !firebaseConfig[key]);
if (missingKeys.length > 0) { if (missingKeys.length > 0) {
console.error(`CRITICAL: Missing Firebase config values: ${missingKeys.join(', ')}`); console.error(`CRITICAL: Missing Firebase config values: ${missingKeys.join(', ')}`);
return false; return false;
} }
try { try {
app = initializeApp(firebaseConfig); app = initializeApp(firebaseConfig);
db = getFirestore(app); db = getFirestore(app);
@@ -117,9 +119,18 @@ const initializeFirebase = () => {
console.error("Error initializing Firebase:", error); console.error("Error initializing Firebase:", error);
return false; return false;
} }
}
// ws / memory mode: stub auth so App's anon-sign-in path works.
const FAKE_USER = { uid: 'local-user', isAnonymous: true };
auth = {
currentUser: FAKE_USER,
};
storage = getStorage();
return true;
}; };
const isFirebaseInitialized = initializeFirebase(); const isInitialized = initializeStorage();
// ============================================================================ // ============================================================================
// FIRESTORE PATH HELPERS // FIRESTORE PATH HELPERS
@@ -2804,12 +2815,20 @@ function App() {
} }
if (!auth) { if (!auth) {
setError("Firebase Auth not initialized. Check your Firebase configuration."); setError("Auth not initialized.");
setIsLoading(false); setIsLoading(false);
setIsAuthReady(false); setIsAuthReady(false);
return; return;
} }
// ws/memory mode: stub auth, no SDK sign-in. Unblock UI immediately.
if (STORAGE_MODE !== 'firebase') {
setUserId(auth.currentUser?.uid || 'local-user');
setIsAuthReady(true);
setIsLoading(false);
return;
}
const initAuth = async () => { const initAuth = async () => {
try { try {
const token = window.__initial_auth_token; const token = window.__initial_auth_token;
@@ -2843,11 +2862,11 @@ function App() {
return () => unsubscribe(); return () => unsubscribe();
}, []); }, []);
if (!isFirebaseInitialized || !db || !auth) { if (!isInitialized || !auth) {
return ( return (
<ErrorDisplay <ErrorDisplay
critical critical
message="Firebase is not properly configured or initialized. Please check your .env.local file and ensure all REACT_APP_FIREBASE_... variables are correctly set." message={`${STORAGE_MODE === 'firebase' ? 'Firebase' : 'Storage'} is not properly configured. Check your .env.local file and ensure all REACT_APP_* variables are correctly set.`}
/> />
); );
} }