UNPKG

naim-firebase-auth-wrapper

Version:

React components and hooks for Firebase Authentication and Firestore with Mantine UI

139 lines 5.39 kB
import { initializeApp, getApps } from 'firebase/app'; import { getAuth } from 'firebase/auth'; import { getFirestore, connectFirestoreEmulator } from 'firebase/firestore'; import { getFirebaseConfig } from '../utils/env'; // Store Firebase instances let firebaseApp = null; let firebaseAuth = null; let firestoreDb = null; let initialized = false; // Getter functions export const getApp = () => { if (firebaseApp) { return firebaseApp; } const existingApps = getApps(); if (existingApps.length > 0) { return existingApps[0]; } return null; }; export const getFirebaseAuth = () => { if (firebaseAuth) { return firebaseAuth; } const app = getApp(); if (app) { return getAuth(app); } return null; }; export const getDb = () => { if (firestoreDb) { return firestoreDb; } const app = getApp(); if (app) { return getFirestore(app); } return null; }; // For backward compatibility export const app = getApp(); export const auth = getFirebaseAuth(); export const db = getDb(); /** * Initialize Firebase with the provided configuration or environment variables */ export const initializeFirebase = (providedConfig) => { if (initialized) { return { app: firebaseApp, auth: firebaseAuth, db: firestoreDb }; } try { // Get Firebase configuration const firebaseConfig = getFirebaseConfig(providedConfig); if (!firebaseConfig) { console.error('[Firebase Config] No valid Firebase configuration found'); return { app: null, auth: null, db: null }; } // Check if Firebase is already initialized const existingApps = getApps(); // If there are existing apps, use the first one if (existingApps.length > 0) { const app = existingApps[0]; const auth = getAuth(app); const db = getFirestore(app); // Store the instances firebaseApp = app; firebaseAuth = auth; firestoreDb = db; initialized = true; return { app, auth, db }; } // If running in a test environment, use a unique app name to prevent conflicts const appName = process.env.NODE_ENV === 'test' ? `test-${Date.now()}` : undefined; // Initialize Firebase with the configuration try { // Ensure all required fields are present if (!firebaseConfig.apiKey || !firebaseConfig.projectId || !firebaseConfig.authDomain) { console.error('[Firebase Config] Missing required Firebase configuration fields'); return { app: null, auth: null, db: null }; } // Initialize Firebase const app = initializeApp(firebaseConfig, appName); const auth = getAuth(app); const db = getFirestore(app); // Store the instances firebaseApp = app; firebaseAuth = auth; firestoreDb = db; initialized = true; // Connect to emulator if specified if (typeof window !== 'undefined' && window.location.hostname === 'localhost' && process.env.NEXT_PUBLIC_USE_FIREBASE_EMULATOR === 'true') { const host = process.env.NEXT_PUBLIC_FIREBASE_EMULATOR_HOST || 'localhost'; const port = process.env.NEXT_PUBLIC_FIREBASE_EMULATOR_PORT || '8081'; connectFirestoreEmulator(db, host, parseInt(port, 10)); console.log(`[Firebase Config] Connected to Firestore emulator at ${host}:${port}`); } return { app, auth, db }; } catch (error) { console.error('[Firebase Config] Error initializing Firebase:', error); // Try to initialize with minimal configuration console.log('[Firebase Config] Attempting to initialize with minimal configuration'); try { const minimalConfig = { apiKey: firebaseConfig.apiKey || 'demo-api-key', authDomain: firebaseConfig.authDomain || 'demo-project.firebaseapp.com', projectId: firebaseConfig.projectId || 'demo-project', }; const app = initializeApp(minimalConfig, appName); const auth = getAuth(app); const db = getFirestore(app); // Store the instances firebaseApp = app; firebaseAuth = auth; firestoreDb = db; initialized = true; return { app, auth, db }; } catch (minimalError) { console.error('[Firebase Config] Error initializing with minimal configuration:', minimalError); return { app: null, auth: null, db: null }; } } } catch (error) { console.error('[Firebase Config] Error in initializeFirebase:', error); return { app: null, auth: null, db: null }; } }; /** * NOTE: For server-side usage (Next.js API routes, etc.), Firebase Admin SDK initialization * should be handled by the consuming application, not this library. * * For server-side functionality, import from 'naim-firebase-auth-wrapper/server' instead. */ //# sourceMappingURL=config.js.map