|
| 1 | +# Firebase Services |
| 2 | + |
| 3 | +VueFire provides a set of composable functions to access some of the different Firebase services. These are all exposed as composables starting with the word _use_: |
| 4 | + |
| 5 | +```vue |
| 6 | +<script setup> |
| 7 | +import { |
| 8 | + useFirebaseApp, |
| 9 | + useAuth, |
| 10 | + useDatabase, |
| 11 | + useFirestore, |
| 12 | + useFirebaseStorage, |
| 13 | + // etc |
| 14 | +} from 'vuefire' |
| 15 | +
|
| 16 | +const firebaseApp = useFirebaseApp() |
| 17 | +const auth = useAuth() |
| 18 | +const database = useDatabase() |
| 19 | +const firestore = useFirestore() |
| 20 | +const storage = useFirebaseStorage() |
| 21 | +</script> |
| 22 | +``` |
| 23 | + |
| 24 | +As [all composables](https://vuejs.org/guide/reusability/composables.html), these must be called within _Injectable Contexts_ like the _setup_ of a component, a Pinia store, or a Router navigation guard. However, you can call these specific Firebase Services composables anywhere in your application as long as you pass the **Firebase App name as the parameter**. |
| 25 | + |
| 26 | +::: tip |
| 27 | +The Firebase Name parameter is only needed when using the composable outside of _setup_ and one of these condition are met: |
| 28 | + |
| 29 | +- You are doing SSR |
| 30 | +- You have multiple Firebase Apps |
| 31 | + |
| 32 | +**Omit the name in all other scenarios**, it's just not needed. |
| 33 | +::: |
| 34 | + |
| 35 | +## Other Firebase Services |
| 36 | + |
| 37 | +For any other service not covered by VueFire, you should use the Firebase SDK directly by passing the firebase app as the first parameter: |
| 38 | + |
| 39 | +```vue |
| 40 | +<script setup> |
| 41 | +import { useFirebaseApp } from 'vuefire' |
| 42 | +import { getMessaging } from 'firebase/messaging' |
| 43 | +
|
| 44 | +const firebaseApp = useFirebaseApp() |
| 45 | +const messaging = getMessaging(firebaseApp) |
| 46 | +</script> |
| 47 | +``` |
| 48 | + |
| 49 | +In the case of Services that require initialization, you should do it alongside the initialization of the Firebase App: |
| 50 | + |
| 51 | +```ts |
| 52 | +import { initializeApp } from 'firebase/app' |
| 53 | +import { initializeAnalytics } from 'firebase/analytics' |
| 54 | + |
| 55 | +export const firebaseApp = initializeApp({ |
| 56 | + // config |
| 57 | +}) |
| 58 | +initializeAnalytics(firebaseApp) |
| 59 | +``` |
| 60 | + |
| 61 | +## Nuxt |
| 62 | + |
| 63 | +In the context of Nuxt, you can create a plugin in the `plugins/` folder, it will be picked up automatically by Nuxt: |
| 64 | + |
| 65 | +::: code-group |
| 66 | + |
| 67 | +```ts [plugins/analytics.client.ts] |
| 68 | +import { |
| 69 | + type Analytics, |
| 70 | + initializeAnalytics, |
| 71 | + isSupported, |
| 72 | +} from 'firebase/analytics' |
| 73 | + |
| 74 | +export default defineNuxtPlugin(async () => { |
| 75 | + const firebaseApp = useFirebaseApp() |
| 76 | + |
| 77 | + console.log('Loading analytics') |
| 78 | + |
| 79 | + let analytics: Analytics | null = null |
| 80 | + if (await isSupported()) { |
| 81 | + analytics = initializeAnalytics(firebaseApp) |
| 82 | + console.log('Loaded analytics') |
| 83 | + } else { |
| 84 | + console.log('Analytics not supported') |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + provide: { |
| 89 | + analytics, |
| 90 | + }, |
| 91 | + } |
| 92 | +}) |
| 93 | +``` |
| 94 | + |
| 95 | +::: |
| 96 | + |
| 97 | +The `.client` suffix is important for services that only run on the client, like analytics. For services that also run on the server, you should omit it.server` suffix. |
0 commit comments