xref: /expo/docs/pages/guides/using-firebase.mdx (revision c615fa2e)
1---
2title: Using Firebase
3maxHeadingDepth: 4
4description: A guide on getting started and using Firebase JS SDK and React Native Firebase library.
5---
6
7import { Terminal } from '~/ui/components/Snippet';
8import { BoxLink } from '~/ui/components/BoxLink';
9import { Collapsible } from '~/ui/components/Collapsible';
10import { Step } from '~/ui/components/Step';
11
12[Firebase](https://firebase.google.com/) is a Backend-as-a-Service (BaaS) app development platform that provides hosted backend services such as realtime database, cloud storage, authentication, crash reporting, analytics, and so on.
13It is built on Google's infrastructure and scales automatically.
14
15There are two different ways you can use Firebase in your projects:
16
17- Using [Firebase JS SDK](#using-firebase-js-sdk)
18- Using [React Native Firebase](#using-react-native-firebase)
19
20React Native supports both the JS SDK and the native SDK. The following sections will guide you through when to use which SDK and all the configuration steps required to use Firebase in your Expo projects.
21
22## Prerequisites
23
24Before proceeding, make sure that you have created a new Firebase project or have an existing one using the [Firebase console](https://console.firebase.google.com/).
25
26## Using Firebase JS SDK
27
28The [Firebase JS SDK](https://firebase.google.com/docs/web/setup) is a JavaScript library that allows you to interact with Firebase services in your project.
29It supports services such as [Authentication](https://firebase.google.com/docs/auth), [Firestore](https://firebase.google.com/docs/firestore), [Realtime Database](https://firebase.google.com/docs/database), and [Storage](https://firebase.google.com/docs/storage) in a React Native app.
30
31### When to use Firebase JS SDK
32
33You can consider using the Firebase JS SDK when you:
34
35- Want to use Firebase services such as Authentication, Firestore, Realtime Database, and Storage in your app and want to develop your app with [**Expo Go**](/workflow/expo-go/).
36- Want a quick start with Firebase services.
37- Want to create a universal app for Android, iOS, and the web.
38
39#### Caveats
40
41Firebase JS SDK does not support all services for mobile apps. Some of these services are Analytics, Dynamic Links and Crashlytics. See the [React Native Firebase](#using-react-native-firebase) section if you want to use these services.
42
43### Install and initialize Firebase JS SDK
44
45The following sub-sections use `[email protected]`. Expo SDK do not enforce or recommend any specific version of Firebase to use in your app.
46
47If you are using an older version of the firebase library in your project, you may have to adapt the code examples to match the version that you are using with the help of the [Firebase JS SDK documentation](https://github.com/firebase/firebase-js-sdk).
48
49<Step label="1">
50#### Install the SDK
51
52After you have created your [Expo project](/get-started/create-a-new-app/), you can install the Firebase JS SDK using the following command:
53
54<Terminal cmd={['$ npx expo install firebase']} />
55</Step>
56
57<Step label="2">
58#### Initialize the SDK in your project
59
60To initialize the Firebase instance in your Expo project, you must create a config object and pass it to the `initializeApp()` method imported from the `firebase/app` module.
61
62The config object requires an API key and other unique identifiers. To obtain these values, you will have to register a web app in your Firebase project. You can find these instructions in the [Firebase documentation](https://firebase.google.com/docs/web/setup#register-app).
63
64After you have the API key and other identifiers, you can paste the following code snippet by creating a new **firebaseConfig.js** file in your project's root directory or any other directory where you keep the configuration files.
65
66```js firebaseConfig.js
67import { initializeApp } from 'firebase/app';
68
69// Optionally import the services that you want to use
70// import {...} from "firebase/auth";
71// import {...} from "firebase/database";
72// import {...} from "firebase/firestore";
73// import {...} from "firebase/functions";
74// import {...} from "firebase/storage";
75
76// Initialize Firebase
77const firebaseConfig = {
78  apiKey: 'api-key',
79  authDomain: 'project-id.firebaseapp.com',
80  databaseURL: 'https://project-id.firebaseio.com',
81  projectId: 'project-id',
82  storageBucket: 'project-id.appspot.com',
83  messagingSenderId: 'sender-id',
84  appId: 'app-id',
85  measurementId: 'G-measurement-id',
86};
87
88const app = initializeApp(firebaseConfig);
89// For more information on how to access Firebase in your project,
90// see the Firebase documentation: https://firebase.google.com/docs/web/setup#access-firebase
91```
92
93You do not have to install other plugins or configurations to use Firebase JS SDK.
94
95Firebase version 9 provides a modular API. You can directly import any service you want to use from the `firebase` package. For example, if you want to use an authentication service in your project, you can import the `auth` module from the `firebase/auth` package.
96
97</Step>
98
99<Step label="3">
100#### Configure Metro
101
102> **info** If you are using Firebase version `9.7.x` and above, you need to add the following configuration to a **metro.config.js** file to make sure that the Firebase JS SDK is bundled correctly.
103
104Expo CLI uses [Metro](https://facebook.github.io/metro/) to bundle your JavaScript code and assets, and add [support for more file extensions](/guides/customizing-metro/#adding-more-file-extensions-to--assetexts).
105
106Start by generating the template file **metro.config.js** in your project's root directory using the following command:
107
108<Terminal cmd={['$ npx expo customize metro.config.js']} />
109
110Then, update the file with the following configuration:
111
112```js metro.config.js
113const { getDefaultConfig } = require('@expo/metro-config');
114
115const defaultConfig = getDefaultConfig(__dirname);
116defaultConfig.resolver.assetExts.push('cjs');
117
118module.exports = defaultConfig;
119```
120
121</Step>
122
123### Next steps
124
125<BoxLink
126  title="Authentication"
127  description="For more information on how to use Authentication in your project, see Firebase documentation."
128  href="https://firebase.google.com/docs/auth/web/start"
129/>
130
131<BoxLink
132  title="Firestore"
133  description="For more information on how to use Firestore database in your project, see Firebase documentation."
134  href="https://firebase.google.com/docs/firestore/quickstart"
135/>
136
137<BoxLink
138  title="Realtime Database"
139  description="For more information on how to use Realtime Database in your project, see Firebase documentation."
140  href="https://firebase.google.com/docs/database"
141/>
142
143<BoxLink
144  title="Storage"
145  description="For more information on how to use Storage, see Firebase documentation."
146  href="https://firebase.google.com/docs/storage/web/start"
147/>
148
149<BoxLink
150  title="Firebase Storage example"
151  description="Learn how to use Firebase Storage in an Expo project with our example."
152  href="https://github.com/expo/examples/tree/master/with-firebase-storage-upload"
153/>
154
155<BoxLink
156  title="Managing API keys for Firebase projects"
157  description="For more information about managing API Key and unique identifiers in a Firebase project."
158  href="https://firebase.google.com/docs/projects/api-keys"
159/>
160
161<BoxLink
162  title="Migrate from Expo Firebase packages to React Native Firebase"
163  description="For more information on migrating from expo-firebase-analytics or expo-firebase-recaptcha packages to React Native Firebase."
164  href="https://expo.fyi/firebase-migration-guide"
165/>
166
167## Using React Native Firebase
168
169[React Native Firebase](https://rnfirebase.io/) provides access to native code by wrapping the native SDKs for Android and iOS into a JavaScript API.
170Each Firebase service is available as a module that can be added as a dependency to your project. For example, the `auth` module provides access to the Firebase Authentication service.
171
172### When to use React Native Firebase
173
174You can consider using React Native Firebase when:
175
176- Your app requires access to Firebase services not supported by the Firebase JS SDK, such as [Dynamic Links](https://rnfirebase.io/dynamic-links/usage), [Crashlytics](https://rnfirebase.io/crashlytics/usage), and so on.
177  For more information on the additional capabilities offered by the native SDK's, see [React Native Firebase documentation](https://rnfirebase.io/faqs-and-tips#why-react-native-firebase-over-firebase-js-sdk).
178- You want to use native SDKs in your app.
179- You have a bare React Native app with React Native Firebase already configured but are migrating to use Expo SDK.
180- You want to use [Firebase Analytics](https://rnfirebase.io/analytics/usage) in your app.
181
182<Collapsible summary="Migrating from Expo Firebase packages?">
183
184If your project has been previously using `expo-firebase-analytics` and `expo-firebase-recaptcha` packages, you can migrate to the React Native Firebase library. For more information, see [Firebase migration guide](https://expo.fyi/firebase-migration-guide).
185
186</Collapsible>
187
188#### Caveats
189
190React Native Firebase requires [custom native code and cannot be used with Expo Go](/workflow/expo-go/#custom-native-code).
191
192### Install and initialize React Native Firebase
193
194<Step label="1">
195#### Install expo-dev-client
196
197Since React Native Firebase requires custom native code, you need to install the `expo-dev-client` library in your project.
198It also allows configuring any native code required by React Native Firebase using [Config plugins](/guides/config-plugins/) without writing native code yourself.
199
200To install [`expo-dev-client`](/development/getting-started/#installing--expo-dev-client--in-your-project), run the following command in your project:
201
202<Terminal cmd={['$ npx expo install expo-dev-client']} />
203</Step>
204
205<Step label="2">
206#### Install React Native Firebase
207
208To use React Native Firebase, it is necessary to install the `@react-native-firebase/app` module. This module provides the core functionality for all other modules.
209It also adds custom native code in your project using a config plugin. You can install it using the following command:
210
211<Terminal cmd={['$ npx expo install @react-native-firebase/app']} />
212
213**At this point, you must follow the instructions from [React Native Firebase documentation](https://rnfirebase.io/#managed-workflow)** as it covers all the steps required to configure your project with the library.
214
215Once you have configured the React Native Firebase library in your project, come back to this guide to learn how to run your project in the next step.
216
217</Step>
218
219<Step label="3">
220#### Run the project
221
222If you are using **[EAS Build](/build/introduction/), you can create and install a development build** on your devices. You do not need to run the project locally before creating a development build.
223For more information on creating a development build, see the section on [installing a development build](/development/create-development-builds/#create-and-install-eas-build).
224
225<Collapsible summary="Run project locally?">
226
227If you want to run the project locally:
228
229- You need both Android Studio and Xcode installed and configured on your computer.
230- Then, you can run the project using `npx expo run:android` or `npx expo run:ios` command.
231
232If a particular React Native Firebase module requires custom native configuration steps, you must add it as a `plugin` to [Expo config file](/workflow/configuration/).
233Then, to run the project locally, you will have to run the `npx expo prebuild --clean` command to apply the native changes before the `npx expo run` commands.
234
235</Collapsible>
236</Step>
237
238### Next steps
239
240After configuring React Native Firebase library, you can use any module it provides in your Expo project.
241
242<BoxLink
243  title="React Native Firebase documentation"
244  description="For more information to install and use a certain module from React Native Firebase, we recommend you to check their documentation."
245  href="https://rnfirebase.io/"
246/>
247