| import { initializeApp } from 'firebase/app'; |
| import { getAuth, GoogleAuthProvider, signInWithPopup, signOut } from 'firebase/auth'; |
| import { getFirestore, doc, getDocFromServer } from 'firebase/firestore'; |
|
|
| const firebaseConfig = { |
| apiKey: import.meta.env.VITE_FIREBASE_API_KEY || "dummy-api-key", |
| authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN || "dummy-auth-domain", |
| projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID || "dummy-project-id", |
| storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET || "dummy-storage-bucket", |
| messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID || "dummy-sender-id", |
| appId: import.meta.env.VITE_FIREBASE_APP_ID || "dummy-app-id", |
| measurementId: import.meta.env.VITE_FIREBASE_MEASUREMENT_ID || "dummy-measurement-id" |
| }; |
|
|
| |
| const isDummy = firebaseConfig.apiKey === "dummy-api-key" || !firebaseConfig.apiKey; |
| if (isDummy) { |
| console.log("Firebase is running in Local Workspace Mode (No API keys provided). Data will be saved locally."); |
| } |
|
|
| const app = initializeApp(firebaseConfig); |
| export const auth = getAuth(app); |
| export const db = getFirestore(app, import.meta.env.VITE_FIREBASE_FIRESTORE_DATABASE_ID || '(default)'); |
| export const googleProvider = new GoogleAuthProvider(); |
|
|
| export const signInWithGoogle = async () => { |
| if (isDummy) { |
| throw new Error("LocalMode"); |
| } |
| try { |
| const result = await signInWithPopup(auth, googleProvider); |
| return result.user; |
| } catch (error) { |
| console.error("Error signing in with Google", error); |
| throw error; |
| } |
| }; |
|
|
| export const logOut = async () => { |
| if (isDummy) return; |
| try { |
| await signOut(auth); |
| } catch (error) { |
| console.error("Error signing out", error); |
| throw error; |
| } |
| }; |
|
|
| |
| async function testConnection() { |
| if (isDummy) return; |
| try { |
| await getDocFromServer(doc(db, 'test', 'connection')); |
| } catch (error) { |
| if(error instanceof Error && error.message.includes('the client is offline')) { |
| console.warn("Firebase client is offline. Defaulting to local mode."); |
| } |
| } |
| } |
| testConnection(); |
|
|