|
1 | 1 | import { initializeApp, getApps } from "firebase/app"; |
| 2 | +import type { FirebaseApp } from "firebase/app"; |
2 | 3 | import { getAuth, onAuthStateChanged } from "firebase/auth"; |
| 4 | +import { useAuthStore } from "../stores/auth"; |
3 | 5 |
|
4 | 6 | export default defineNuxtPlugin(() => { |
5 | 7 | const config = useRuntimeConfig(); |
| 8 | + const publicConfig = config.public as Record<string, string>; |
6 | 9 |
|
7 | 10 | // Skip initialization if no API key is configured |
8 | | - if (!config.public.firebaseApiKey) { |
| 11 | + if (!publicConfig.firebaseApiKey) { |
9 | 12 | console.warn( |
10 | 13 | "[firebase] No FIREBASE_API_KEY configured. Auth will not work.", |
11 | 14 | ); |
12 | 15 | return; |
13 | 16 | } |
14 | 17 |
|
15 | 18 | const firebaseConfig = { |
16 | | - apiKey: config.public.firebaseApiKey, |
17 | | - authDomain: config.public.firebaseAuthDomain, |
18 | | - projectId: config.public.firebaseProjectId, |
19 | | - storageBucket: config.public.firebaseStorageBucket, |
20 | | - messagingSenderId: config.public.firebaseMessagingSenderId, |
21 | | - appId: config.public.firebaseAppId, |
22 | | - measurementId: config.public.firebaseMeasurementId, |
| 19 | + apiKey: publicConfig.firebaseApiKey, |
| 20 | + authDomain: publicConfig.firebaseAuthDomain, |
| 21 | + projectId: publicConfig.firebaseProjectId, |
| 22 | + storageBucket: publicConfig.firebaseStorageBucket, |
| 23 | + messagingSenderId: publicConfig.firebaseMessagingSenderId, |
| 24 | + appId: publicConfig.firebaseAppId, |
| 25 | + measurementId: publicConfig.firebaseMeasurementId, |
23 | 26 | }; |
24 | 27 |
|
25 | 28 | // Initialize Firebase (only once) |
26 | | - const app = |
| 29 | + const app: FirebaseApp = |
27 | 30 | getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0]; |
28 | 31 | const auth = getAuth(app); |
29 | 32 |
|
30 | 33 | // Also initialize the compat SDK for FirebaseUI |
31 | 34 | if (import.meta.client) { |
32 | | - import("firebase/compat/app").then((firebase) => { |
33 | | - if (!firebase.default.apps.length) { |
34 | | - firebase.default.initializeApp(firebaseConfig); |
35 | | - } |
36 | | - }); |
| 35 | + import("firebase/compat/app").then( |
| 36 | + (firebase: { |
| 37 | + default: { |
| 38 | + apps: unknown[]; |
| 39 | + initializeApp: (config: typeof firebaseConfig) => void; |
| 40 | + }; |
| 41 | + }) => { |
| 42 | + if (!firebase.default.apps.length) { |
| 43 | + firebase.default.initializeApp(firebaseConfig); |
| 44 | + } |
| 45 | + }, |
| 46 | + ); |
37 | 47 | } |
38 | 48 |
|
39 | 49 | // Listen for auth state changes and update the auth store |
|
0 commit comments