xref: /expo/docs/pages/guides/using-firebase.mdx (revision 7fbf9884)
1---
2title: Use 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**](/get-started/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-project/), 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> Using Firebase Authentication with version 9 or below? See [the guide for setting up persistence to keep users logged in between reloads](https://expo.fyi/firebase-js-auth-setup).
98
99</Step>
100
101<Step label="3">
102#### Configure Metro
103
104> **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.
105
106Expo 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).
107
108Start by generating the template file **metro.config.js** in your project's root directory using the following command:
109
110<Terminal cmd={['$ npx expo customize metro.config.js']} />
111
112Then, update the file with the following configuration:
113
114```js metro.config.js
115const { getDefaultConfig } = require('@expo/metro-config');
116
117const defaultConfig = getDefaultConfig(__dirname);
118defaultConfig.resolver.sourceExts.push('cjs');
119
120module.exports = defaultConfig;
121```
122
123</Step>
124
125### Next steps
126
127<BoxLink
128  title="Authentication"
129  description="For more information on how to use Authentication in your project, see Firebase documentation."
130  href="https://firebase.google.com/docs/auth/web/start"
131/>
132
133<BoxLink
134  title="Firestore"
135  description="For more information on how to use Firestore database in your project, see Firebase documentation."
136  href="https://firebase.google.com/docs/firestore/quickstart"
137/>
138
139<BoxLink
140  title="Realtime Database"
141  description="For more information on how to use Realtime Database in your project, see Firebase documentation."
142  href="https://firebase.google.com/docs/database"
143/>
144
145<BoxLink
146  title="Storage"
147  description="For more information on how to use Storage, see Firebase documentation."
148  href="https://firebase.google.com/docs/storage/web/start"
149/>
150
151<BoxLink
152  title="Firebase Storage example"
153  description="Learn how to use Firebase Storage in an Expo project with our example."
154  href="https://github.com/expo/examples/tree/master/with-firebase-storage-upload"
155/>
156
157<BoxLink
158  title="Managing API keys for Firebase projects"
159  description="For more information about managing API Key and unique identifiers in a Firebase project."
160  href="https://firebase.google.com/docs/projects/api-keys"
161/>
162
163<BoxLink
164  title="Migrate from Expo Firebase packages to React Native Firebase"
165  description="For more information on migrating from expo-firebase-analytics or expo-firebase-recaptcha packages to React Native Firebase."
166  href="https://expo.fyi/firebase-migration-guide"
167/>
168
169## Using React Native Firebase
170
171[React Native Firebase](https://rnfirebase.io/) provides access to native code by wrapping the native SDKs for Android and iOS into a JavaScript API.
172Each 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.
173
174### When to use React Native Firebase
175
176You can consider using React Native Firebase when:
177
178- 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.
179  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).
180- You want to use native SDKs in your app.
181- You have a bare React Native app with React Native Firebase already configured but are migrating to use Expo SDK.
182- You want to use [Firebase Analytics](https://rnfirebase.io/analytics/usage) in your app.
183
184<Collapsible summary="Migrating from Expo Firebase packages?">
185
186If 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).
187
188</Collapsible>
189
190#### Caveats
191
192React Native Firebase requires [custom native code and cannot be used with Expo Go](/workflow/customizing/).
193
194### Install and initialize React Native Firebase
195
196<Step label="1">
197#### Install expo-dev-client
198
199Since React Native Firebase requires custom native code, you need to install the `expo-dev-client` library in your project.
200It also allows configuring any native code required by React Native Firebase using [Config plugins](/config-plugins/introduction/) without writing native code yourself.
201
202To install [`expo-dev-client`](/development/getting-started/#installing--expo-dev-client--in-your-project), run the following command in your project:
203
204<Terminal cmd={['$ npx expo install expo-dev-client']} />
205</Step>
206
207<Step label="2">
208#### Install React Native Firebase
209
210To 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.
211It also adds custom native code in your project using a config plugin. You can install it using the following command:
212
213<Terminal cmd={['$ npx expo install @react-native-firebase/app']} />
214
215**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.
216
217Once 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.
218
219</Step>
220
221<Step label="3">
222#### Run the project
223
224If 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.
225For more information on creating a development build, see the section on [installing a development build](/develop/development-builds/create-a-build/).
226
227<Collapsible summary="Run project locally?">
228
229If you want to run the project locally:
230
231- You need both Android Studio and Xcode installed and configured on your computer.
232- Then, you can run the project using `npx expo run:android` or `npx expo run:ios` command.
233
234If a particular React Native Firebase module requires custom native configuration steps, you must add it as a `plugin` to [app config file](/workflow/configuration/).
235Then, 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.
236
237</Collapsible>
238</Step>
239
240### Next steps
241
242After configuring React Native Firebase library, you can use any module it provides in your Expo project.
243
244<BoxLink
245  title="React Native Firebase documentation"
246  description="For more information to install and use a certain module from React Native Firebase, we recommend you to check their documentation."
247  href="https://rnfirebase.io/"
248/>
249