|
1 | 1 | import { initializeApp } from "firebase/app"; |
2 | | -import { getFirestore } from "firebase/firestore"; |
3 | | -import { getAuth } from "firebase/auth"; |
| 2 | +import { Firestore, getFirestore } from "firebase/firestore"; |
| 3 | +import { Auth, getAuth } from "firebase/auth"; |
| 4 | +import { Analytics, getAnalytics } from "firebase/analytics"; |
4 | 5 |
|
5 | | -const firebaseConfig = { |
6 | | - apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, |
7 | | - authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, |
8 | | - projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, |
9 | | - storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, |
10 | | - messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, |
11 | | - appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID, |
12 | | - measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID, |
13 | | -}; |
| 6 | +let auth: Auth | undefined; |
| 7 | +let db: Firestore | undefined; |
| 8 | +let analytics: Analytics | undefined; |
14 | 9 |
|
15 | | -const app = initializeApp(firebaseConfig); |
| 10 | +try { |
| 11 | + const firebaseConfig = { |
| 12 | + apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, |
| 13 | + authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, |
| 14 | + projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, |
| 15 | + storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, |
| 16 | + messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, |
| 17 | + appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID, |
| 18 | + measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID, |
| 19 | + }; |
16 | 20 |
|
17 | | -export const auth = getAuth(app); |
18 | | -export const db = getFirestore(app); |
| 21 | + const app = initializeApp(firebaseConfig); |
| 22 | + |
| 23 | + auth = getAuth(app); |
| 24 | + db = getFirestore(app); |
| 25 | + analytics = typeof window !== "undefined" ? getAnalytics(app) : undefined; |
| 26 | +} catch (error) { |
| 27 | + console.log( |
| 28 | + Error( |
| 29 | + "Error while setting up firebase. Please add .env file with required credentials for firebase setup" |
| 30 | + ) |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +export { auth, db, analytics }; |
0 commit comments