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
30 changes: 15 additions & 15 deletions packages/dart_firebase_admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class:
final app = FirebaseApp.initializeApp();
```

This will automatically initalize 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.
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.

### Initialize the SDK in non-Google environments

Expand Down Expand Up @@ -81,19 +81,19 @@ Once you have initialized an app instance with a credential, you can use any of
final app = FirebaseApp.initializeApp();

// Getting a user by id
final user = await app.auth.getUser("<user-id>");
final user = await app.auth().getUser("<user-id>");

// Deleting a user by id
await app.auth.deleteUser("<user-id>");
await app.auth().deleteUser("<user-id>");

// Listing users
final result = await app.auth.listUsers(maxResults: 10, pageToken: null);
final result = await app.auth().listUsers(maxResults: 10, pageToken: null);
final users = result.users;
final nextPageToken = result.pageToken;

// Verifying an ID token (e.g. from request headers) from a client application
final idToken = req.headers['Authorization'].split(' ')[1];
final decodedToken = await app.auth.verifyIdToken(idToken, checkRevoked: true);
final decodedToken = await app.auth().verifyIdToken(idToken, checkRevoked: true);
final userId = decodedToken.uid;
```

Expand All @@ -103,11 +103,11 @@ final userId = decodedToken.uid;
final app = FirebaseApp.initializeApp();

// Verifying an app check token
final response = await app.appCheck.verifyToken("<appCheckToken>");
final response = await app.appCheck().verifyToken("<appCheckToken>");
print("App ID: ${response.appId}");

// Creating a new app check token
final result = await app.appCheck.createToken("<app-id>");
final result = await app.appCheck().createToken("<app-id>");
print("Token: ${result.token}");
```

Expand All @@ -117,20 +117,20 @@ print("Token: ${result.token}");
final app = FirebaseApp.initializeApp();

// Getting a document
final snapshot = await app.firestore.collection("users").doc("<user-id>").get();
final snapshot = await app.firestore().collection("users").doc("<user-id>").get();
print(snapshot.data());

// Querying a collection
final snapshot = await app.firestore.collection("users")
final snapshot = await app.firestore().collection("users")
.where('age', .greaterThan, 18)
.orderBy('age', descending: true)
.get();
print(snapshot.docs())

// Running a transaction (e.g. adding credits to a balance)
final balance = await app.firestore.runTransaction((tsx) async {
final balance = await app.firestore().runTransaction((tsx) async {
// Get a reference to a user document
final ref = app.firestore.collection("users").doc("<user-id>");
final ref = app.firestore().collection("users").doc("<user-id>");

// Get the document data
final snapshot = await tsx.get(ref);
Expand All @@ -156,7 +156,7 @@ final balance = await app.firestore.runTransaction((tsx) async {
final app = FirebaseApp.initializeApp();

// Get a task queue by name
final queue = app.functions.taskQueue("<task-name>");
final queue = app.functions().taskQueue("<task-name>");

// Add data to the queue
await queue.enqueue({ "hello": "world" });
Expand All @@ -168,7 +168,7 @@ await queue.enqueue({ "hello": "world" });
final app = FirebaseApp.initializeApp();

// Send a message to a specific device
await app.messaging.send(
await app.messaging().send(
TokenMessage(
token: "<device-token>",
data: { "hello": "world" },
Expand All @@ -177,7 +177,7 @@ await app.messaging.send(
);

// Send a message to a topic
await app.messaging.send(
await app.messaging().send(
TopicMessage(
topic: "<topic-name>",
data: { "hello": "world" },
Expand All @@ -186,7 +186,7 @@ await app.messaging.send(
);

// Send a message to a conditional statement
await app.messaging.send(
await app.messaging().send(
ConditionMessage(
condition: "\'stock-GOOG\' in topics || \'industry-tech\' in topics",
data: { "hello": "world" },
Expand Down
15 changes: 6 additions & 9 deletions packages/dart_firebase_admin/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:dart_firebase_admin/auth.dart';
import 'package:dart_firebase_admin/dart_firebase_admin.dart';
import 'package:dart_firebase_admin/firestore.dart';
import 'package:dart_firebase_admin/functions.dart';
import 'package:dart_firebase_admin/messaging.dart';

Expand Down Expand Up @@ -32,7 +31,7 @@ Future<void> main() async {
Future<void> authExample(FirebaseApp admin) async {
print('\n### Auth Example ###\n');

final auth = Auth(admin);
final auth = admin.auth();

UserRecord? user;
try {
Expand Down Expand Up @@ -62,7 +61,7 @@ Future<void> authExample(FirebaseApp admin) async {
Future<void> firestoreExample(FirebaseApp admin) async {
print('\n### Firestore Example ###\n');

final firestore = Firestore(admin);
final firestore = admin.firestore();

try {
final collection = firestore.collection('users');
Expand All @@ -80,8 +79,7 @@ Future<void> firestoreExample(FirebaseApp admin) async {
Future<void> projectConfigExample(FirebaseApp admin) async {
print('\n### Project Config Example ###\n');

final auth = Auth(admin);
final projectConfigManager = auth.projectConfigManager;
final projectConfigManager = admin.auth().projectConfigManager;

try {
// Get current project configuration
Expand Down Expand Up @@ -156,8 +154,7 @@ Future<void> projectConfigExample(FirebaseApp admin) async {
Future<void> tenantExample(FirebaseApp admin) async {
print('\n### Tenant Example ###\n');

final auth = Auth(admin);
final tenantManager = auth.tenantManager;
final tenantManager = admin.auth().tenantManager;

String? createdTenantId;

Expand Down Expand Up @@ -246,7 +243,7 @@ Future<void> tenantExample(FirebaseApp admin) async {
Future<void> messagingExample(FirebaseApp admin) async {
print('\n### Messaging Example ###\n');

final messaging = Messaging(admin);
final messaging = admin.messaging();

// Example 1: Send a message to a topic
try {
Expand Down Expand Up @@ -402,7 +399,7 @@ Future<void> messagingExample(FirebaseApp admin) async {
Future<void> functionsExample(FirebaseApp admin) async {
print('\n### Functions Example ###\n');

final functions = Functions(admin);
final functions = admin.functions();

// Get a task queue reference
// The function name should match an existing Cloud Function or queue name
Expand Down
4 changes: 2 additions & 2 deletions packages/dart_firebase_admin/example_server_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
git:
url: https://github.com/invertase/dart_firebase_admin.git
path: packages/dart_firebase_admin
ref: messaging/apis
ref: next
shelf: ^1.4.2
shelf_router: ^1.1.2

Expand All @@ -18,7 +18,7 @@ dependency_overrides:
git:
url: https://github.com/invertase/dart_firebase_admin.git
path: packages/googleapis_auth_utils
ref: messaging/apis
ref: next

dev_dependencies:
http: ^1.2.2
Expand Down
20 changes: 10 additions & 10 deletions packages/dart_firebase_admin/lib/src/app/firebase_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,42 +151,42 @@ class FirebaseApp {
/// Gets the App Check service instance for this app.
///
/// Returns a cached instance if one exists, otherwise creates a new one.
AppCheck get appCheck =>
getOrInitService(FirebaseServiceType.appCheck.name, AppCheck.new);
AppCheck appCheck() =>
getOrInitService(FirebaseServiceType.appCheck.name, AppCheck.internal);

/// Gets the Auth service instance for this app.
///
/// Returns a cached instance if one exists, otherwise creates a new one.
Auth get auth => getOrInitService(FirebaseServiceType.auth.name, Auth.new);
Auth auth() => getOrInitService(FirebaseServiceType.auth.name, Auth.internal);

/// Gets the Firestore service instance for this app.
///
/// Returns a cached instance if one exists, otherwise creates a new one.
/// Optional [settings] are only applied when creating a new instance.
Firestore firestore({Settings? settings}) => getOrInitService(
FirebaseServiceType.firestore.name,
(app) => Firestore(app, settings: settings),
(app) => Firestore.internal(app, settings: settings),
);

/// Gets the Messaging service instance for this app.
///
/// Returns a cached instance if one exists, otherwise creates a new one.
Messaging get messaging =>
getOrInitService(FirebaseServiceType.messaging.name, Messaging.new);
Messaging messaging() =>
getOrInitService(FirebaseServiceType.messaging.name, Messaging.internal);

/// Gets the Security Rules service instance for this app.
///
/// Returns a cached instance if one exists, otherwise creates a new one.
SecurityRules get securityRules => getOrInitService(
SecurityRules securityRules() => getOrInitService(
FirebaseServiceType.securityRules.name,
SecurityRules.new,
SecurityRules.internal,
);

/// Gets the Functions service instance for this app.
///
/// Returns a cached instance if one exists, otherwise creates a new one.
Functions get functions =>
getOrInitService(FirebaseServiceType.functions.name, Functions.new);
Functions functions() =>
getOrInitService(FirebaseServiceType.functions.name, Functions.internal);

/// Closes this app and cleans up all associated resources.
///
Expand Down
13 changes: 2 additions & 11 deletions packages/dart_firebase_admin/lib/src/app_check/app_check.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ part 'app_check_request_handler.dart';

class AppCheck implements FirebaseService {
/// Creates or returns the cached AppCheck instance for the given app.
factory AppCheck(FirebaseApp app) {
return app.getOrInitService(FirebaseServiceType.appCheck.name, AppCheck._);
}

AppCheck._(this.app)
: _requestHandler = AppCheckRequestHandler(app),
_tokenGenerator = AppCheckTokenGenerator(app.createCryptoSigner()),
_appCheckTokenVerifier = AppCheckTokenVerifier(app);

@internal
factory AppCheck.internal(
FirebaseApp app, {
Expand All @@ -37,7 +28,7 @@ class AppCheck implements FirebaseService {
}) {
return app.getOrInitService(
FirebaseServiceType.appCheck.name,
(app) => AppCheck._internal(
(app) => AppCheck._(
app,
requestHandler: requestHandler,
tokenGenerator: tokenGenerator,
Expand All @@ -46,7 +37,7 @@ class AppCheck implements FirebaseService {
);
}

AppCheck._internal(
AppCheck._(
this.app, {
AppCheckRequestHandler? requestHandler,
AppCheckTokenGenerator? tokenGenerator,
Expand Down
11 changes: 2 additions & 9 deletions packages/dart_firebase_admin/lib/src/auth/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ part of '../auth.dart';
/// An Auth instance can have multiple tenants.
class Auth extends _BaseAuth implements FirebaseService {
/// Creates or returns the cached Auth instance for the given app.
factory Auth(FirebaseApp app) {
return app.getOrInitService(FirebaseServiceType.auth.name, Auth._);
}

Auth._(FirebaseApp app)
: super(app: app, authRequestHandler: AuthRequestHandler(app));

@internal
factory Auth.internal(
FirebaseApp app, {
Expand All @@ -20,7 +13,7 @@ class Auth extends _BaseAuth implements FirebaseService {
}) {
return app.getOrInitService(
FirebaseServiceType.auth.name,
(app) => Auth._internal(
(app) => Auth._(
app,
requestHandler: requestHandler,
idTokenVerifier: idTokenVerifier,
Expand All @@ -29,7 +22,7 @@ class Auth extends _BaseAuth implements FirebaseService {
);
}

Auth._internal(
Auth._(
FirebaseApp app, {
AuthRequestHandler? requestHandler,
super.idTokenVerifier,
Expand Down
15 changes: 3 additions & 12 deletions packages/dart_firebase_admin/lib/src/functions/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,19 @@ const _emulatedServiceAccountDefault = 'emulated-service-acct@email.com';
/// those tasks before they execute.
class Functions implements FirebaseService {
/// Creates or returns the cached Functions instance for the given app.
factory Functions(FirebaseApp app) {
return app.getOrInitService(
FirebaseServiceType.functions.name,
Functions._,
);
}

/// An interface for interacting with Cloud Functions Task Queues.
Functions._(this.app) : _requestHandler = FunctionsRequestHandler(app);

@internal
factory Functions.internal(
FirebaseApp app, {
FunctionsRequestHandler? requestHandler,
}) {
return app.getOrInitService(
FirebaseServiceType.functions.name,
(app) => Functions._internal(app, requestHandler: requestHandler),
(app) => Functions._(app, requestHandler: requestHandler),
);
}

Functions._internal(this.app, {FunctionsRequestHandler? requestHandler})
/// An interface for interacting with Cloud Functions Task Queues.
Functions._(this.app, {FunctionsRequestHandler? requestHandler})
: _requestHandler = requestHandler ?? FunctionsRequestHandler(app);

/// The app associated with this Functions instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Firestore implements FirebaseService {
///
/// Note: Settings can only be specified on the first call. Subsequent calls
/// will return the cached instance and ignore any new settings.
factory Firestore(FirebaseApp app, {Settings? settings}) {
@internal
factory Firestore.internal(FirebaseApp app, {Settings? settings}) {
return app.getOrInitService(
FirebaseServiceType.firestore.name,
(app) => Firestore._(app, settings: settings),
Expand Down
20 changes: 4 additions & 16 deletions packages/dart_firebase_admin/lib/src/messaging/messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,20 @@ const _fmcMaxBatchSize = 500;
/// An interface for interacting with the Firebase Cloud Messaging service.
class Messaging implements FirebaseService {
/// Creates or returns the cached Messaging instance for the given app.
factory Messaging(FirebaseApp app) {
return app.getOrInitService(
FirebaseServiceType.messaging.name,
Messaging._,
);
}

/// An interface for interacting with the Firebase Cloud Messaging service.
Messaging._(this.app)
: _requestHandler = FirebaseMessagingRequestHandler(app);

@internal
factory Messaging.internal(
FirebaseApp app, {
FirebaseMessagingRequestHandler? requestHandler,
}) {
return app.getOrInitService(
FirebaseServiceType.messaging.name,
(app) => Messaging._internal(app, requestHandler: requestHandler),
(app) => Messaging._(app, requestHandler: requestHandler),
);
}

Messaging._internal(
this.app, {
FirebaseMessagingRequestHandler? requestHandler,
}) : _requestHandler = requestHandler ?? FirebaseMessagingRequestHandler(app);
/// An interface for interacting with the Firebase Cloud Messaging service.
Messaging._(this.app, {FirebaseMessagingRequestHandler? requestHandler})
: _requestHandler = requestHandler ?? FirebaseMessagingRequestHandler(app);

/// The app associated with this Messaging instance.
@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class TopicMessage extends Message {

/// A message targeting a condition.
///
/// See [Send messages to topics](https://firebase.google.com/docs/cloud-messaging/send-message#send-messages-to-topics).
/// See [Send to topic conditions](https://firebase.google.com/docs/cloud-messaging/send-topic-messages).
class ConditionMessage extends Message {
ConditionMessage({
required this.condition,
Expand Down
Loading
Loading