|
| 1 | +// Firebase initialization wrapper for browser environment |
| 2 | +// This file initializes Firebase using the modular SDK |
| 3 | + |
| 4 | +// Check if we're in a browser and Firebase can be loaded |
| 5 | +window.firebaseReady = false; |
| 6 | +window.firebaseError = null; |
| 7 | + |
| 8 | +// Firebase configuration |
| 9 | +const firebaseConfig = { |
| 10 | + apiKey: "AIzaSyBBM7OVM3-jwHky-1P_6JL4RAmzhebuDPg", |
| 11 | + authDomain: "prompt-sharing-f8eeb.firebaseapp.com", |
| 12 | + projectId: "prompt-sharing-f8eeb", |
| 13 | + storageBucket: "prompt-sharing-f8eeb.firebasestorage.app", |
| 14 | + messagingSenderId: "840037476057", |
| 15 | + appId: "1:840037476057:web:a8ca03b1defe94071e80c8", |
| 16 | + measurementId: "G-8QR07NVLML" |
| 17 | +}; |
| 18 | + |
| 19 | +// Initialize Firebase when modular SDK is available |
| 20 | +function initFirebaseWhenReady() { |
| 21 | + try { |
| 22 | + // Using the global firebase namespace that should be available after SDK loads |
| 23 | + if (typeof firebase !== 'undefined' && firebase.initializeApp) { |
| 24 | + // Initialize app |
| 25 | + const app = firebase.initializeApp(firebaseConfig); |
| 26 | + console.log('Firebase initialized successfully'); |
| 27 | + |
| 28 | + // Get services - compat API doesn't require app parameter |
| 29 | + window.auth = firebase.auth(); |
| 30 | + window.db = firebase.firestore(); |
| 31 | + window.functions = firebase.functions(); |
| 32 | + |
| 33 | + // For local development, allow localhost |
| 34 | + if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') { |
| 35 | + if (window.auth.settings) { |
| 36 | + window.auth.settings.appVerificationDisabledForTesting = true; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + window.firebaseReady = true; |
| 41 | + console.log('Firebase services ready:', { auth: !!window.auth, db: !!window.db, functions: !!window.functions }); |
| 42 | + |
| 43 | + return true; |
| 44 | + } else { |
| 45 | + return false; |
| 46 | + } |
| 47 | + } catch (error) { |
| 48 | + console.error('Firebase initialization error:', error); |
| 49 | + window.firebaseError = error; |
| 50 | + return false; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Try to initialize immediately |
| 55 | +if (!initFirebaseWhenReady()) { |
| 56 | + // If not ready, retry every 100ms for up to 30 seconds |
| 57 | + let attempts = 0; |
| 58 | + const maxAttempts = 300; |
| 59 | + const retryInterval = setInterval(() => { |
| 60 | + attempts++; |
| 61 | + if (initFirebaseWhenReady() || attempts >= maxAttempts) { |
| 62 | + clearInterval(retryInterval); |
| 63 | + if (attempts >= maxAttempts) { |
| 64 | + console.error('Failed to initialize Firebase after 30 seconds'); |
| 65 | + window.firebaseError = 'Timeout waiting for Firebase SDK'; |
| 66 | + } |
| 67 | + } |
| 68 | + }, 100); |
| 69 | +} |
| 70 | + |
| 71 | +// Also expose a manual check function |
| 72 | +window.checkFirebaseReady = function() { |
| 73 | + return window.firebaseReady; |
| 74 | +}; |
0 commit comments