NOVAESPORT / src /firebase.ts
Musabek's picture
Update codebase from AI Studio Build
797cc76
Raw History Blame Contribute Delete
2.04 kB
import { initializeApp, getApp, getApps } from "firebase/app";
import { getAuth, GoogleAuthProvider } from "firebase/auth";
import { initializeFirestore, doc, getDocFromServer } from "firebase/firestore";
import firebaseConfig from "../firebase-applet-config.json";
// Initialize Firebase safely with automated platform config
const app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();
export const auth = getAuth(app);
export const db = initializeFirestore(app, { experimentalForceLongPolling: true }, firebaseConfig.firestoreDatabaseId); // CRITICAL: Pass database ID and enable long-polling in sandboxed environment
export const googleProvider = new GoogleAuthProvider();
async function testConnection() {
try {
await getDocFromServer(doc(db, 'test', 'connection'));
} catch (error) {
if (error instanceof Error && error.message.includes('the client is offline')) {
console.warn("Please check your Firebase configuration; we are currently offline.");
}
}
}
testConnection();
// Standard Firebase Firestore Error Handler as specified in Skill
export enum OperationType {
CREATE = 'create',
UPDATE = 'update',
DELETE = 'delete',
LIST = 'list',
GET = 'get',
WRITE = 'write',
}
interface FirestoreErrorInfo {
error: string;
operationType: OperationType;
path: string | null;
authInfo: {
userId?: string | null;
email?: string | null;
emailVerified?: boolean | null;
isAnonymous?: boolean | null;
}
}
export function handleFirestoreError(error: unknown, operationType: OperationType, path: string | null) {
const errInfo: FirestoreErrorInfo = {
error: error instanceof Error ? error.message : String(error),
authInfo: {
userId: auth.currentUser?.uid,
email: auth.currentUser?.email,
emailVerified: auth.currentUser?.emailVerified,
isAnonymous: auth.currentUser?.isAnonymous,
},
operationType,
path
};
console.error('Firestore Error: ', JSON.stringify(errInfo));
throw new Error(JSON.stringify(errInfo));
}