You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make sure that your server runs **Dart SDK 3.9 or higher**.
60
+
61
+
### Set up a Firebase project and service account
62
+
63
+
To use the Firebase Admin SDK, you'll need the following:
64
+
65
+
- A Firebase project.
66
+
- A Firebase Admin SDK service account to communicate with Firebase. This service account is created automatically when you create a Firebase project or add Firebase to a Google Cloud project.
67
+
- A configuration file with your service account's credentials.
68
+
69
+
If you don't already have a Firebase project, you need to create one in the [Firebase console](https://console.firebase.google.com/). Visit [Understand Firebase Projects](https://firebase.google.com/docs/projects/learn-more) to learn more about Firebase projects.
70
+
71
+
### Add the SDK
72
+
73
+
If you are setting up a new project, you need to install the SDK for the language of your choice.
74
+
75
+
The Firebase Admin Dart SDK is available on [pub.dev](https://pub.dev/) as `dart_firebase_admin`:
76
+
77
+
```bash
78
+
$ dart pub add dart_firebase_admin
79
+
```
80
+
81
+
To use the module in your application, `import` it from any Dart file:
To initalize the Firebase Admin SDK, call the `initializeApp` method on the `Firebase`
60
-
class:
89
+
Once you have created a Firebase project, you can initialize the SDK with Google Application Default Credentials. Because default credentials lookup is fully automated in Google environments, with no need to supply environment variables or other configuration, this way of initializing the SDK is strongly recommended for applications running in Google environments such as Firebase App Hosting, Cloud Run, App Engine, and Cloud Functions for Firebase.
61
90
62
91
```dart
63
92
final app = FirebaseApp.initializeApp();
64
93
```
65
94
66
-
This will automatically initialize the SDK with [Google Application Default Credentials](https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application). Because default credentials lookup is fully automated in Google environments, with no need to supply environment variables or other configuration, this way of initializing the SDK is strongly recommended for applications running in Google environments such as Firebase App Hosting, Cloud Run, App Engine, and Cloud Functions for Firebase.
95
+
To optionally specify initialization options for services such as Realtime Database, Cloud Storage, or Cloud Functions, use the `FIREBASE_CONFIG` environment variable. If the content of the `FIREBASE_CONFIG` variable begins with a `{` it will be parsed as a JSON object. Otherwise the SDK assumes that the string is the path of a JSON file containing the options.
67
96
68
-
To optionally specify initialization options, use the `FIREBASE_CONFIG` environment variable. If the content begins with `{` it will be parsed as a JSON object, otherwise it is treated as a path to a JSON file containing the options:
97
+
> **Note:** The `FIREBASE_CONFIG` environment variableis included automatically in App Hosting backends and Cloud Functions for Firebase functions.
@@ -80,6 +109,28 @@ final app = FirebaseApp.initializeApp();
80
109
81
110
If you are working in a non-Google server environment in which default credentials lookup can't be fully automated, you can initialize the SDK with an exported service account key file.
82
111
112
+
> **Important:** Extremely high security awareness is required when working with service account keys, as they are vulnerable to certain types of threats. See [Best practices for managing service account keys](https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys).
113
+
114
+
Firebase projects support Google service accounts, which you can use to call Firebase server APIs from your app server or trusted environment. If you're developing code locally or deploying your application on-premises, you can use credentials obtained via this service account to authorize server requests.
115
+
116
+
To generate a private key file for your service account:
117
+
118
+
1. In the Firebase console, open **Settings > Service Accounts**.
119
+
2. Click **Generate New Private Key**, then confirm by clicking **Generate Key**.
120
+
3. Securely store the JSON file containing the key.
121
+
122
+
When authorizing via a service account, you have two choices for providing the credentials to your application. You can either set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, or you can explicitly pass the path to the service account key in code. The first option is more secure and is strongly recommended.
123
+
124
+
To set the environment variable:
125
+
126
+
Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.
After you've completed the above steps, Application Default Credentials (ADC) is able to implicitly determine your credentials, allowing you to use service account credentials when testing or running in non-Google environments.
133
+
83
134
The `initializeApp` method allows for creating multiple named app instances and specifying a custom credential, project ID and other options:
84
135
85
136
```dart
@@ -136,7 +187,7 @@ final defaultAuth = defaultApp.auth();
136
187
final defaultFirestore = defaultApp.firestore();
137
188
```
138
189
139
-
Some use cases require multiple app instances at the same time — for example, reading data from one Firebase project and creating custom tokens for another, or authenticating two apps with separate credentials:
190
+
Some use cases require you to create multiple apps at the same time. For example, you might want to read data from the Realtime Database of one Firebase project and mint custom tokens for another project. Or you might want to authenticate two apps with separate credentials. The Firebase SDK allows you create multiple apps at the same time, each with their own configuration information.
140
191
141
192
```dart
142
193
// Each AppOptions points to a different Firebase project
@@ -167,9 +218,18 @@ final otherAuth = otherApp.auth();
167
218
final otherFirestore = otherApp.firestore();
168
219
```
169
220
221
+
> **Note:** Each app instance has its own configuration options and authentication state.
222
+
170
223
### Testing with gcloud end user credentials
171
224
172
-
When testing locally with Google Application Default Credentials obtained via `gcloud auth application-default login`, you must explicitly provide a project ID because Firebase Authentication does not accept gcloud end-user credentials without one:
225
+
When testing the Admin SDK locally with Google Application Default Credentials obtained by running `gcloud auth application-default login`, additional changes are needed to use Firebase Authentication due to the following:
226
+
227
+
- Firebase Authentication does not accept gcloud end user credentials generated using the gcloud OAuth client ID.
228
+
- Firebase Authentication requires the project ID to be provided on initialization for these type of end user credentials.
229
+
230
+
You can specify the project ID explicitly on app initialization or just use the `GOOGLE_CLOUD_PROJECT` environment variable. The latter avoids the need to make any additional changes to test your code.
0 commit comments