Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "dart-firebase-admin"
}
}
3 changes: 0 additions & 3 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions README.md
7 changes: 7 additions & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": false,
".write": false
}
}
23 changes: 23 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"emulators": {
"auth": {
"port": 9099
},
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"database": {
"port": 9000
},
"pubsub": {
"port": 8085
},
"ui": {
"enabled": true
},
"singleProjectMode": true
}
}
4 changes: 4 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"indexes": [],
"fieldOverrides": []
}
19 changes: 19 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
rules_version = '2';

service cloud.firestore {
match /databases/{database}/documents {

// This rule allows anyone with your Firestore database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write: if request.time < timestamp.date(2023, 8, 30);
}
}
}
157 changes: 157 additions & 0 deletions packages/dart_firebase_admin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
## Dart Firebase Admin

Welcome! This project is a port of [Node's Fireabse Admin SDK](https://github.com/firebase/firebase-admin-node) to Dart.

⚠️ This project is still in its early stages, and some features may be missing or bugged.
Currently, only Firestore is available, with more to come (auth next).

- [Dart Firebase Admin](#dart-firebase-admin)
- [Usage](#usage)
- [Connecting to the SDK](#connecting-to-the-sdk)
- [Connecting using the environment](#connecting-using-the-environment)
- [Connecting using a \`service-account.json\`\` file](#connecting-using-a-service-accountjson-file)
- [Using Firestore](#using-firestore)

## Usage

### Connecting to the SDK

Before using Firebase, we must first authenticate.

There are currently two options:

- You can connect using environment variables
- Alternatively, you can specify a `service-account.json` file

#### Connecting using the environment

To connect using environment variables, you will need to have
the [Firebase CLI](https://firebaseopensource.com/projects/firebase/firebase-tools/) installed.

Once done, you can run:

```sh
firebase login
```

And log-in to the project of your choice.

From there, you can have your Dart program authenticate
using the environment with:

```dart
import 'package:dart_firebase_admin/dart_firebase_admin.dart';

void main() {
final admin = FirebaseAdminApp.initializeApp(
'<your project name>',
// This will obtain authentication informations from the environment
Credential.fromApplicationDefaultCredentials(),
);

// TODO use the Admin SDK
final firestore = Firestore(admin);
firestore.doc('hello/world').get();
}
```

#### Connecting using a `service-account.json`` file

Alternatively, you can choose to use a `service-account.json` file.
This file can be obtained in your firebase console by going to:

```
https://console.firebase.google.com/u/0/project/<your-project-name>/settings/serviceaccounts/adminsdk
```

Make sure to replace `<your-project-name>` with the name of your project.
One there, follow the steps and download the file. Place it anywhere you want in your project.

**⚠️ Note**:
This file should be kept private. Do not commit it on public repositories.

After all of that is done, you can now authenticate in your Dart program using:

```dart
import 'package:dart_firebase_admin/dart_firebase_admin.dart';

void main() {
final admin = FirebaseAdminApp.initializeApp(
'<your project name>',
// Log-in using the newly downloaded file.
Credential.fromServiceAccount(
File('<path to your service-account.json file>'),
),
);

// TODO use the Admin SDK
final firestore = Firestore(admin);
firestore.doc('hello/world').get();
}
```

### Using Firestore

First, make sure to follow the steps on [how to authenticate](#connecting-to-the-sdk).
You should now have an instance of a `FirebaseAdminApp` object.

You can now use this object to create a `Firestore` object as followed:

```dart
// Obtained in the previous steps
FirebaseAdminApp admin;
final firestore = Firestore(admin);
```

From this point onwards, using Firestore with the admin ADK
is roughtly equivalent to using [FlutterFire](https://github.com/firebase/flutterfire).

Using this `Firestore` object, you'll find your usual collection/query/document
objects.

For example you can perform a `where` query:

```dart
// The following lists all users above 18 years old
final collection = firestore.collection('users');
final adults = collection.where('age', WhereFilter.greaterThan, 18);

final adultsSnapshot = await adults.get();

for (final adult in adultsSnapshot.docs) {
print(adult.data()['age']);
}
```

Composite queries are also supported:

```dart
// List users with either John or Jack as first name.
firestore
.collection('users')
.whereFilter(
Filter.or([
Filter.where('firstName', WhereFilter.equal, 'John'),
Filter.where('firstName', WhereFilter.equal, 'Jack'),
]),
);
```

Alternatively, you can fetch a specific document too:

```dart
// Print the age of the user with ID "123"
final user = await firestore.doc('users/123').get();
print(user.data()?['age']);
```

---

<p align="center">
<a href="https://invertase.io/?utm_source=readme&utm_medium=footer&utm_campaign=dart_custom_lint">
<img width="75px" src="https://static.invertase.io/assets/invertase/invertase-rounded-avatar.png">
</a>
<p align="center">
Built and maintained by <a href="https://invertase.io/?utm_source=readme&utm_medium=footer&utm_campaign=dart_custom_lint">Invertase</a>.
</p>
</p>
23 changes: 16 additions & 7 deletions packages/dart_firebase_admin/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import 'package:dart_firebase_admin/dart_firebase_admin.dart';
import 'package:dart_firebase_admin/firestore.dart';

Future<void> main() async {
final admin = FirebaseAdminApp.initializeApp(
'dart-firebase-admin',
Credential.fromApplicationDefaultCredentials(),
);

final auth = FirebaseAdminAuth(admin);
admin.useEmulator();

// await auth.deleteUser('867gK70vkJNjOzlj4uQoMcg7a1d2');
// await auth.createSessionCookie('867gK70vkJNjOzlj4uQoMcg7a1d2');
final d = await auth.deleteUsers(['p9bj9If2i4eQlr7NxnaxWGZsmgq1']);
print(d.errors);
print(d.failureCount);
print('Deleted!');
final firestore = Firestore(admin);

final collection = firestore.collection('users');

await collection.doc('123').set({
'name': 'John Doe',
'age': 30,
});

final snapshot = await collection.get();

for (final doc in snapshot.docs) {
print(doc.data());
}
}
22 changes: 1 addition & 21 deletions packages/dart_firebase_admin/lib/dart_firebase_admin.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
library dart_firebase_admin;

import 'dart:convert';
import 'dart:io';

import 'package:firebaseapis/identitytoolkit/v1.dart' as firebase_auth_v1;
import 'package:firebaseapis/identitytoolkit/v2.dart' as firebase_auth_v2;
import 'package:firebaseapis/identitytoolkit/v3.dart' as firebase_auth_v3;
import 'package:googleapis_auth/auth_io.dart' as auth;

part 'src/auth/auth_exception.dart';
part 'src/auth/create_request.dart';
part 'src/auth/delete_users_result.dart';
part 'src/auth/firebase_admin_auth.dart';
part 'src/auth/update_request.dart';
part 'src/auth/user_info.dart';
part 'src/auth/user_metadata.dart';
part 'src/auth/user_record.dart';
part 'src/credential.dart';
part 'src/exception.dart';
part 'src/firebase_admin.dart';
export 'src/dart_firebase_admin.dart' show FirebaseAdminApp, Credential;
2 changes: 2 additions & 0 deletions packages/dart_firebase_admin/lib/firestore.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'src/google_cloud_firestore/firestore.dart'
hide $SettingsCopyWith, DocumentReader, ApiMapValue;
Loading