Skip to content

Commit 691662a

Browse files
authored
chore: update README sections (#201)
1 parent 556706f commit 691662a

1 file changed

Lines changed: 85 additions & 11 deletions

File tree

packages/dart_firebase_admin/README.md

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
## Table of Contents
44

5-
- [Prerequisites](#prerequisites)
65
- [Overview](#overview)
76
- [Installation](#installation)
87
- [Add the Firebase Admin SDK to your server](#add-the-firebase-admin-sdk-to-your-server)
8+
- [Prerequisites](#prerequisites)
9+
- [Set up a Firebase project and service account](#set-up-a-firebase-project-and-service-account)
10+
- [Add the SDK](#add-the-sdk)
911
- [Initialize the SDK](#initialize-the-sdk)
1012
- [Initialize the SDK in non-Google environments](#initialize-the-sdk-in-non-google-environments)
1113
- [Using Workload Identity Federation](#using-workload-identity-federation)
@@ -24,10 +26,6 @@
2426
- [Contributing](#contributing)
2527
- [License](#license)
2628

27-
## Prerequisites
28-
29-
Make sure that your server runs **Dart SDK 3.9 or higher**.
30-
3129
## Overview
3230

3331
[Firebase](https://firebase.google.com) provides the tools and infrastructure
@@ -54,18 +52,49 @@ import 'package:dart_firebase_admin/dart_firebase_admin.dart';
5452

5553
## Add the Firebase Admin SDK to your server
5654

55+
### Prerequisites
56+
57+
Make sure that you have a server app.
58+
59+
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:
82+
83+
```dart
84+
import 'package:dart_firebase_admin/dart_firebase_admin.dart';
85+
```
86+
5787
### Initialize the SDK
5888

59-
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.
6190

6291
```dart
6392
final app = FirebaseApp.initializeApp();
6493
```
6594

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.
6796

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 variable is included automatically in App Hosting backends and Cloud Functions for Firebase functions.
6998
7099
```bash
71100
export FIREBASE_CONFIG='{"projectId":"my-project"}'
@@ -80,6 +109,28 @@ final app = FirebaseApp.initializeApp();
80109

81110
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.
82111

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.
127+
128+
```bash
129+
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
130+
```
131+
132+
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+
83134
The `initializeApp` method allows for creating multiple named app instances and specifying a custom credential, project ID and other options:
84135

85136
```dart
@@ -136,7 +187,7 @@ final defaultAuth = defaultApp.auth();
136187
final defaultFirestore = defaultApp.firestore();
137188
```
138189

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.
140191

141192
```dart
142193
// Each AppOptions points to a different Firebase project
@@ -167,9 +218,18 @@ final otherAuth = otherApp.auth();
167218
final otherFirestore = otherApp.firestore();
168219
```
169220

221+
> **Note:** Each app instance has its own configuration options and authentication state.
222+
170223
### Testing with gcloud end user credentials
171224

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.
231+
232+
To explicitly specify the project ID:
173233

174234
```dart
175235
final app = FirebaseApp.initializeApp(
@@ -427,6 +487,20 @@ await queue.enqueue(
427487
);
428488
```
429489

490+
#### enqueue with TaskOptionsExperimental
491+
492+
```dart
493+
// Route the task to a custom URI (beta feature)
494+
await queue.enqueue(
495+
{'action': 'customHandler'},
496+
TaskOptions(
497+
experimental: TaskOptionsExperimental(
498+
uri: 'https://my-service.example.com/task-handler',
499+
),
500+
),
501+
);
502+
```
503+
430504
#### delete
431505

432506
```dart

0 commit comments

Comments
 (0)