Git Product home page Git Product logo

dart_firebase_admin's Introduction

Dart Firebase Admin

Welcome! This project is a port of Node's Firebase Admin SDK 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).

Getting started

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

Once done, you can run:

firebase login

And log-in to the project of your choice.

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

import 'package:dart_firebase_admin/dart_firebase_admin.dart';

void main() {
  final admin = FirebaseAdminApp.initializeApp(
    '<your project name>',
    // This will obtain authentication information 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:

import 'package:dart_firebase_admin/dart_firebase_admin.dart';

Future<void> main() async {
  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();

  // Don't forget to close the Admin SDK at the end of your "main"!
  await admin.close();
}

Firestore

Usage

First, make sure to follow the steps on how to authenticate. You should now have an instance of a FirebaseAdminApp object.

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

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

From this point onwards, using Firestore with the admin ADK is roughly equivalent to using FlutterFire.

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

For example you can perform a where query:

// 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:

// 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:

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

Supported features

Firestore
reference.id โœ…
reference.parent โœ…
reference.path โœ…
reference.== โœ…
reference.withConverter โœ…
collection.listDocuments โœ…
collection.add โœ…
collection.get โœ…
collection.create โœ…
collection.delete โœ…
collection.set โœ…
collection.update โœ…
collection.collection โœ…
query.where('field', operator, value) โœ…
query.where('field.path', operator, value) โœ…
query.where(FieldPath('...'), operator, value) โœ…
query.whereFilter(Filter.and(a, b)) โœ…
query.whereFilter(Filter.or(a, b)) โœ…
query.startAt โœ…
query.startAtDocument โœ…
query.startAfter โœ…
query.startAfterDocument โœ…
query.endAt โœ…
query.endAtDocument โœ…
query.endAfter โœ…
query.endAfterDocument โœ…
query.select โœ…
query.orderBy โœ…
query.limit โœ…
query.limitToLast โœ…
query.offset โœ…
querySnapshot.docs โœ…
querySnapshot.readTime โœ…
documentSnapshots.data โœ…
documentSnapshots.readTime/createTime/updateTime โœ…
documentSnapshots.id โœ…
documentSnapshots.exists โœ…
documentSnapshots.data โœ…
documentSnapshots.get(fieldPath) โœ…
FieldValue.documentId โœ…
FieldValue.increment โœ…
FieldValue.arrayUnion โœ…
FieldValue.arrayRemove โœ…
FieldValue.delete โœ…
FieldValue.serverTimestamp โœ…
collectionGroup โœ…
GeoPoint โœ…
Timestamp โœ…
querySnapshot.docsChange โš ๏ธ
query.onSnapshot โŒ
runTransaction โŒ
BundleBuilder โŒ

Auth

Usage

First, make sure to follow the steps on how to authenticate. You should now have an instance of a FirebaseAdminApp object.

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

// Obtained in the previous steps
FirebaseAdminApp admin;
final auth = FirebaseAuth(admin);

You can then use this FirebaseAuth object to perform various auth operations. For example, you can generate a password reset link:

final link = await auth.generatePasswordResetLink(
  '[email protected]',
);

Supported features

Available features

Auth
auth.tenantManager โŒ
auth.projectConfigManager โŒ
auth.generatePasswordResetLink โœ…
auth.generateEmailVerificationLink โœ…
auth.generateVerifyAndChangeEmailLink โœ…
auth.generateSignInWithEmailLink โœ…
auth.listProviderConfigs โœ…
auth.createProviderConfig โœ…
auth.updateProviderConfig โœ…
auth.getProviderConfig โœ…
auth.deleteProviderConfig โœ…
auth.createCustomToken โœ…
auth.setCustomUserClaims โœ…
auth.verifyIdToken โœ…
auth.revokeRefreshTokens โœ…
auth.createSessionCookie โœ…
auth.verifySessionCookie โœ…
auth.importUsers โœ…
auth.listUsers โœ…
auth.deleteUser โœ…
auth.deleteUsers โœ…
auth.getUser โœ…
auth.getUserByPhoneNumber โœ…
auth.getUserByEmail โœ…
auth.getUserByProviderUid โœ…
auth.getUsers โœ…
auth.createUser โœ…
auth.updateUser โœ…

Messaging

Usage

First, make sure to follow the steps on how to authenticate. You should now have an instance of a FirebaseAdminApp object.

Then, you can create an instance of Messaging as followed:

// Obtained in the previous steps
FirebaseAdminApp admin;
final messaging = Messaging(messaging);

You can then use that Messaging object to interact with Firebase Messaging. For example, if you want to send a notification to a specific device, you can do:

await messaging.send(
  TokenMessage(
    // The token of the targeted device.
    // This token can be obtain by using FlutterFire's firebase_messaging:
    // https://pub.dev/documentation/firebase_messaging/latest/firebase_messaging/FirebaseMessaging/getToken.html
    token: "<targeted device's token>",
    notification: Notification(
      // The content of the notification
      title: 'Hello',
      body: 'World',
    ),
  ),
);

Supported features

Messaging
Messaging.send โœ…
Messaging.sendEach โœ…
Messaging.sendEachForMulticast โœ…
Messaging.subscribeToTopic โŒ
Messaging.unsubscribeFromTopic โŒ
TokenMessage โœ…
TopicMessage โœ…
ConditionMessage โœ…
Messaging.sendAll โŒ
Messaging.sendMulticast โŒ

Built and maintained by Invertase.

dart_firebase_admin's People

Contributors

rrousselgit avatar ehesp avatar lesnitsky avatar codekeyz avatar erickzanardo avatar doctorjohn avatar akaboshinit avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.