forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.client.ts
More file actions
47 lines (41 loc) · 1.45 KB
/
Copy pathfirebase.client.ts
File metadata and controls
47 lines (41 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { initializeApp, getApps } from 'firebase/app'
import type { FirebaseApp } from 'firebase/app'
import { getAuth, onAuthStateChanged } from 'firebase/auth'
import { useAuthStore } from '../stores/auth'
export default defineNuxtPlugin(() => {
const config = useRuntimeConfig()
const publicConfig = config.public as Record<string, string>
// Skip initialization if no API key is configured
if (!publicConfig.firebaseApiKey) {
console.warn(
'[firebase] No FIREBASE_API_KEY configured. Auth will not work.',
)
// Resolve auth readiness so route middleware doesn't hang forever.
useAuthStore().onAuthStateChanged(null)
return
}
const firebaseConfig = {
apiKey: publicConfig.firebaseApiKey,
authDomain: publicConfig.firebaseAuthDomain,
projectId: publicConfig.firebaseProjectId,
storageBucket: publicConfig.firebaseStorageBucket,
messagingSenderId: publicConfig.firebaseMessagingSenderId,
appId: publicConfig.firebaseAppId,
measurementId: publicConfig.firebaseMeasurementId,
}
// Initialise Firebase (only once)
const app: FirebaseApp =
getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0]!
const auth = getAuth(app)
// Listen for auth state changes and update the auth store
const authStore = useAuthStore()
onAuthStateChanged(auth, (user) => {
authStore.onAuthStateChanged(user)
})
return {
provide: {
firebaseApp: app,
firebaseAuth: auth,
},
}
})