ng add @angular/fire-## Realtime bindings and authentication for Angular 2 +- **Dependency injection** - Provide and Inject Firebase services in your components. +- **Zone.js wrappers** - Stable zones allow proper functionality of service workers, forms, SSR, and pre-rendering. +- **Observable based** - Utilize RxJS rather than callbacks for real-time streams. +- **NgRx friendly API** - Integrate with NgRx using AngularFire's action based APIs. +- **Lazy-loading** - AngularFire dynamically imports much of Firebase, reducing the time to load your app. +- **Deploy schematics** - Get your Angular application deployed on Firebase Hosting with a single command. +- **Google Analytics** - Zero-effort Angular Router awareness in Google Analytics. +- **Router Guards** - Guard your Angular routes with built-in Firebase Authentication checks. -AngularFire2 integrates Firebase's realtime observers and authentication with Angular2. - -### Example use: +## Example use ```ts -import {Component} from 'angular2/core'; -import {AngularFire} from 'angularfire2'; -import {Observable} from 'rxjs/Observable'; - -@Component({ - selector: 'project-name-app', - providers: [], - templateUrl: 'app/project-name-app.html', - directives: [], - pipes: [] +import { provideFirebaseApp, initializeApp } from '@angular/fire/app'; +import { getFirestore, provideFirestore } from '@angular/fire/firestore'; + +export const appConfig: ApplicationConfig = { + providers: [ + provideFirebaseApp(() => initializeApp({ ... })), + provideFirestore(() => getFirestore()), + ... + ], + ... }) -export class MyApp { - items: Observable
| +#### [Analytics](docs/analytics.md#analytics) +```ts +import { } from '@angular/fire/analytics'; ``` + | +-### FIREBASE_PROVIDERS - -Contains all AngularFire provider configuration for Angular's dependency injection. - -Type: `any[]` - -Usage: - +#### [Authentication](docs/auth.md#authentication) +```ts +import { } from '@angular/fire/auth'; ``` -import {bootstrap} from 'angular2/platform/browser'; -import {App} from './app'; -import {FIREBASE_PROVIDERS} from 'angularfire2'; + | +
| -bootstrap(App, FIREBASE_PROVIDERS); +#### [Cloud Firestore](docs/firestore.md#cloud-firestore) +```ts +import { } from '@angular/fire/firestore'; ``` + | +-### defaultFirebase - -Define the root url for the library, to resolve relative paths. - -Type: `string` - -Usage: - +#### [Cloud Functions](docs/functions.md#cloud-functions) +```ts +import { } from '@angular/fire/functions'; ``` -import {bootstrap} from 'angular2/platform/browser'; -import {FIREBASE_PROVIDERS, defaultFirebase} from 'angularfire2'; + | +
| -bootstrap(App, [ - FIREBASE_PROVIDERS, - defaultFirebase('https://my.firebaseio.com') -]); +#### [Cloud Messaging](docs/messaging.md#cloud-messaging) +```ts +import { } from '@angular/fire/messaging'; ``` + | +-### FirebaseRef - -Injectable symbol to create a Firebase reference based on -the url provided by `FirebaseUrl`. - -Type: `Firebase` - -Usage: - -``` -import {Inject} from 'angular2/core'; -import {FirebaseRef} from 'angularfire2'; -... -class MyComponent { - constructor(@Inject(FirebaseRef) ref:Firebase) { - ref.on('value', this.doSomething); - } -} +#### [Cloud Storage](docs/storage.md#cloud-storage) +```ts +import { } from '@angular/fire/storage'; ``` + | +
| -### FirebaseUrl - -URL for the app's default Firebase database. - -Type: `string` - -Usage: - +#### [Performance Monitoring](docs/performance.md#performance-monitoring) +```ts +import { } from '@angular/fire/performance'; ``` -import {bootstrap} from 'angular2/platform/browser'; -import {Inject} from 'angular2/core'; -import {FirebaseUrl, FIREBASE_PROVIDERS, defaultFirebase} from 'angularfire2'; + | +-@Component({ - selector: 'app', - template: `{{ url }}` -}) -class App { - constructor(@Inject(FirebaseUrl) public url: string) {} -} - -bootstrap(App, [ - FIREBASE_PROVIDERS, - defaultFirebase('https://my.firebaseio.com') -]); +#### [Realtime Database](docs/database.md#realtime-database) +```ts +import { } from '@angular/fire/database'; ``` + | +
|
-### FirebaseAuth
-
-Injectable service for managing authentication state.
-
-#### Logging In
-To log in a user, call the `login` method on an instance of `FirebaseAuth` class. The method has
-the following two signatures:
-
-```typescript
-login(config?: AuthConfiguration): Promise |
+ -@Component({ - selector: 'my-component' - templateUrl: 'my_template.html' -}) -export class MyApp { - constructor (private _auth: FirebaseAuth) {} - - public doLogin () { - // This will perform popup auth with google oauth and the scope will be email - // Because those options were provided through bootstrap to DI, and we're overriding the provider. - this._auth.login({ - provider: AuthProviders.Google - }); - } -} +#### [App Check](docs/app-check.md#app-check) +```ts +import { } from '@angular/fire/app-check'; ``` + | +
|
-#### Subscribing to Authentication State
-
-Type: `class FirebaseAuth extends ReplaySubject You are logged in
- Please log in
- `
-})
-class App {
- constructor (@Inject(FirebaseAuth) public auth: FirebaseAuth) {}
-}
+#### [Vertex AI](docs/vertexai.md#vertex-ai)
+```ts
+import { } from '@angular/fire/vertexai';
```
+ |
-### FirebaseListObservable
-
-Subclass of rxjs `Observable` which also has methods for updating
-list-like Firebase data.
-
-type: `class`
-
-additional methods:
-
-`add:(val) => void`: Add an element to the Firebase ref.
+
+
+
+AngularFire ❱ Developer Guide ❱ Analytics
+
+
+# Analytics
+
+Google Analytics is an app measurement solution, available at no charge, that provides insight on app usage and user engagement.
+
+[Learn more](https://firebase.google.com/docs/analytics)
+
+## Dependency Injection
+
+As a prerequisite, ensure that `AngularFire` has been added to your project via
+```bash
+ng add @angular/fire
+```
+
+Provide a Analytics instance in the application's `app.config.ts`:
+
+```ts
+import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
+import { provideAnalytics, getAnalytics } from '@angular/fire/analytics';
+
+export const appConfig: ApplicationConfig = {
+ providers: [
+ provideFirebaseApp(() => initializeApp({ ... })),
+ provideAnalytics(() => getAnalytics()),
+ ...
+ ],
+ ...,
+}
+```
+
+Next inject `Analytics` into your component:
+
+```typescript
+import { Component, inject } from '@angular/core';
+import { Analytics } from '@angular/fire/analytics';
+
+@Component({ ... })
+export class UserProfileComponent {
+ private analytics = inject(Analytics);
+ ...
+}
+```
+
+## Firebase API
+
+AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API.
+
+Update the imports from `import { ... } from 'firebase/analytics'` to `import { ... } from '@angular/fire/analytics'` and follow the official documentation.
+
+[Getting Started](https://firebase.google.com/docs/analytics/get-started?platform=web) | [API Reference](https://firebase.google.com/docs/reference/js/analytics)
diff --git a/docs/app-check.md b/docs/app-check.md
new file mode 100644
index 000000000..3c6cd43d5
--- /dev/null
+++ b/docs/app-check.md
@@ -0,0 +1,57 @@
+
+AngularFire ❱ Developer Guide ❱ App Check
+
+
+
+
+# App Check
+
+App Check helps protect your API resources from abuse by preventing unauthorized clients from accessing your backend resources. It works with both Firebase services, Google Cloud services, and your own APIs to keep your resources safe.
+
+[Learn More](https://firebase.google.com/docs/app-check)
+
+## Dependency Injection
+
+As a prerequisite, ensure that `AngularFire` has been added to your project via
+```bash
+ng add @angular/fire
+```
+
+Provide a App Check instance in the application's `app.config.ts`:
+
+```ts
+import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
+import { provideAppCheck, initializeAppCheck, ReCaptchaV3Provider } from '@angular/fire/app-check';
+
+export const appConfig: ApplicationConfig = {
+ providers: [
+ provideFirebaseApp(() => initializeApp({ ... })),
+ provideAppCheck(() => initializeAppCheck(getApp(), {
+ provider: new ReCaptchaV3Provider(/* configuration */),
+ })),
+ ...
+ ],
+ ...
+})
+```
+
+Next inject `AppCheck` it into your component:
+
+```ts
+import { Component, inject} from '@angular/core';
+import { AppCheck } from '@angular/fire/app-check';
+
+@Component({ ... })
+export class AppCheckComponent {
+ private appCheck = inject(AppCheck);
+ ...
+}
+```
+
+## Firebase API
+
+AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API.
+
+Update the imports from `import { ... } from 'firebase/app-check'` to `import { ... } from '@angular/fire/app-check'` and follow the official documentation.
+
+[Getting Started](https://firebase.google.com/docs/app-check/web/recaptcha-enterprise-provider) | [API Reference](https://firebase.google.com/docs/reference/js/app-check)
diff --git a/docs/auth.md b/docs/auth.md
new file mode 100644
index 000000000..8ce5a51aa
--- /dev/null
+++ b/docs/auth.md
@@ -0,0 +1,200 @@
+
+
+
+AngularFire ❱ Developer Guide ❱ Authentication
+
+
+# Authentication
+
+Most apps need to know the identity of a user. Knowing a user's identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user's devices.
+Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.
+
+Firebase Authentication integrates tightly with other Firebase services, and it leverages industry standards like OAuth 2.0 and OpenID Connect, so it can be easily integrated with your custom backend.
+
+[Learn more about Firebase Authentication](https://firebase.google.com/docs/auth)
+
+## Dependency Injection
+
+As a prerequisite, ensure that `AngularFire` has been added to your project via
+```bash
+ng add @angular/fire
+```
+
+Provide a Auth instance in the application's `app.config.ts`:
+
+```ts
+import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
+import { provideAuth, getAuth } from '@angular/fire/auth';
+
+export const appConfig: ApplicationConfig = {
+ providers: [
+ provideFirebaseApp(() => initializeApp({ ... })),
+ provideAuth(() => getAuth()),
+ ...
+ ],
+ ...
+})
+```
+
+Next inject `Auth` into your component:
+
+```ts
+import { Component, inject} from '@angular/core';
+import { Auth } from '@angular/fire/auth';
+
+@Component({ ... })
+export class LoginComponent {
+ private auth = inject(Auth);
+ ...
+}
+```
+
+## Firebase API
+
+AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API.
+
+Update the imports from `import { ... } from 'firebase/auth'` to `import { ... } from '@angular/fire/auth'` and follow the official documentation.
+
+[Getting Started](https://firebase.google.com/docs/auth/web/start) | [API Reference](https://firebase.google.com/docs/reference/js/auth)
+
+## Server-side Rendering
+
+To support Auth context in server-side rendering, you can provide `FirebaseServerApp`:
+
+```ts
+import { ApplicationConfig, PLATFORM_ID, inject } from '@angular/core';
+import { isPlatformBrowser } from '@angular/common';
+import { provideFirebaseApp, initializeApp, initializeServeApp, initializeServerApp } from '@angular/fire/app';
+import { provideAuth, getAuth } from '@angular/fire/auth';
+
+export const appConfig: ApplicationConfig = {
+ providers: [
+ provideFirebaseApp(() => {
+ if (isPlatformBrowser(inject(PLATFORM_ID))) {
+ return initializeApp(firebaseConfig);
+ }
+ // Optional, since it's null in dev-mode and SSG
+ const request = inject(REQUEST, { optional: true });
+ const authIdToken = request?.headers.authorization?.split("Bearer ")[1];
+ return initializeServerApp(firebaseConfig, {
+ authIdToken,
+ releaseOnDeref: request || undefined
+ });
+ }),
+ provideAuth(() => getAuth(inject(FirebaseApp)),
+ ...
+ ],
+ ...
+})
+```
+
+Follow Firebase's [ Session Management with Service Workers documentation](https://firebase.google.com/docs/auth/web/service-worker-sessions) to learn how to pass the `idToken` to the server. __Note: this will not currently work in dev-mode as Angular SSR does not provide a method to get the Request headers.__
+
+## Convenience observables
+
+AngularFire provides observables to allow convenient use of the Firebase Authentication with RXJS.
+
+### user
+
+The `user` observable streams events triggered by sign-in, sign-out, and token refresh events.
+
+Example code:
+
+```ts
+import { Auth, User, user } from '@angular/fire/auth';
+...
+
+export class UserComponent implements OnDestroy {
+ private auth: Auth = inject(Auth);
+ user$ = user(auth);
+ userSubscription: Subscription;
+ ...
+
+ constructor() {
+ this.userSubscription = this.user$.subscribe((aUser: User | null) => {
+ //handle user state changes here. Note, that user will be null if there is no currently logged in user.
+ console.log(aUser);
+ })
+ }
+
+ ngOnDestroy() {
+ // when manually subscribing to an observable remember to unsubscribe in ngOnDestroy
+ this.userSubscription.unsubscribe();
+ }
+}
+
+```
+
+### authState
+
+The `authState` observable streams events triggered by sign-in and sign-out events.
+
+Example code:
+```ts
+import { Auth, authState } from '@angular/fire/auth';
+...
+
+export class UserComponent implements OnDestroy {
+ private auth: Auth = inject(Auth);
+ authState$ = authState(auth);
+ authStateSubscription: Subscription;
+ ...
+
+ constructor() {
+ this.authStateSubscription = this.authState$.subscribe((aUser: User | null) => {
+ //handle auth state changes here. Note, that user will be null if there is no currently logged in user.
+ console.log(aUser);
+ })
+ }
+
+ ngOnDestroy() {
+ // when manually subscribing to an observable remember to unsubscribe in ngOnDestroy
+ this.authStateSubscription.unsubscribe();
+ }
+}
+```
+
+### idToken
+
+The `idToken` observable streams events triggered by sign-in, sign-out and token refresh events.
+
+Example code:
+```ts
+import { Auth, idToken } from '@angular/fire/auth';
+...
+
+export class UserComponent implements OnDestroy {
+ private auth: Auth = inject(Auth);
+ idToken$ = idToken(auth);
+ idTokenSubscription: Subscription;
+ ...
+
+ constructor() {
+ this.idTokenSubscription = this.idToken$.subscribe((token: string | null) => {
+ //handle idToken changes here. Note, that user will be null if there is no currently logged in user.
+ console.log(string);
+ })
+ }
+
+ ngOnDestroy() {
+ // when manually subscribing to an observable remember to unsubscribe in ngOnDestroy
+ this.idTokenSubscription.unsubscribe();
+ }
+}
+```
+
+## Connecting the emulator suite
+
+```ts
+import { connectAuthEmulator, getAuth, provideAuth } from '@angular/fire/auth';
+
+@NgModule({
+ imports: [
+ provideAuth(() => {
+ const auth = getAuth();
+ connectAuthEmulator(auth, 'http://localhost:9099', { disableWarnings: true });
+ return auth;
+ }),
+ ]
+})
+```
diff --git a/docs/compat.md b/docs/compat.md
new file mode 100644
index 000000000..ddd89f3b0
--- /dev/null
+++ b/docs/compat.md
@@ -0,0 +1,70 @@
+# AngularFire
+The official [Angular](https://angular.io/) library for [Firebase](https://firebase.google.com/).
+
+ng add @angular/fire+ +## Compatibility Developer Guide + +AngularFire has a new tree-shakable API, you're looking at the documentation for the compatability version of the library. [Find the new developer guide here](../README.md#developer-guide). + +[See the v7 upgrade guide for more information on this change.](version-7-upgrade.md). + +### Monitor usage of your application in production + +> `AngularFireAnalytics` provides a convenient method of interacting with Google Analytics in your Angular application. The provided `ScreenTrackingService` and `UserTrackingService` automatically log events when you're using the Angular Router or Firebase Authentication respectively. [Learn more about Google Analytics](https://firebase.google.com/docs/analytics). + +- [Getting started with Google Analytics](compat/analytics/getting-started.md) + +### Interacting with your database(s) + +Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing. [Learn about the differences between them in the Firebase Documentation](https://firebase.google.com/docs/firestore/rtdb-vs-firestore). + +#### Cloud Firestore + +> `AngularFirestore` allows you to work with Cloud Firestore, the new flagship database for mobile app development. It improves on the successes of Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales better than Realtime Database. + +- [Documents](compat/firestore/documents.md) +- [Collections](compat/firestore/collections.md) +- [Querying Collections](compat/firestore/querying-collections.md) +- [Offline data](compat/firestore/offline-data.md) + +#### Realtime Database + +> `AngularFireDatabase` allows you to work with the Realtime Database, Firebase's original database. It's an efficient, low-latency solution for mobile apps that require synced states across clients in realtime. + +- [Objects](compat/rtdb/objects.md) +- [Lists](compat/rtdb/lists.md) +- [Querying lists](compat/rtdb/querying-lists.md) + +### Authenticate users + +- [Getting started with Firebase Authentication](compat/auth/getting-started.md) +- [Route users with AngularFire guards](compat/auth/router-guards.md) + +### Local Emulator Suite + +- [Getting started with Firebase Emulator Suite](compat/emulators/emulators.md) + +### Upload files + +- [Getting started with Cloud Storage](compat/storage/storage.md) + +### Receive push notifications + +- [Getting started with Firebase Messaging](compat/messaging/messaging.md) + +### **BETA:** Change behavior and appearance of your application without deploying + +> Firebase Remote Config is a cloud service that lets you change the behavior and appearance of your app without requiring users to download an app update. [Learn more about Remote Config](https://firebase.google.com/docs/remote-config). + +- [Getting started with Remote Config](compat/remote-config/getting-started.md) + +### Monitor your application performance in production + +> Firebase Performance Monitoring is a service that helps you to gain insight into the performance characteristics of your iOS, Android, and web apps. [Learn more about Performance Monitoring](https://firebase.google.com/docs/perf-mon). + +- [Getting started with Performance Monitoring](compat/performance/getting-started.md) + +### Directly call Cloud Functions + +- [Getting started with Callable Functions](compat/functions/functions.md) diff --git a/docs/compat/analytics/getting-started.md b/docs/compat/analytics/getting-started.md new file mode 100644 index 000000000..3c5af8aaf --- /dev/null +++ b/docs/compat/analytics/getting-started.md @@ -0,0 +1,137 @@ +# Getting started with Google Analytics + +`AngularFireAnalytics` dynamically imports the `firebase/analytics` library and provides a promisified version of the [Firebase Analytics SDK (`firebase.analytics.Analytics`)](https://firebase.google.com/docs/reference/js/firebase.analytics.Analytics.html). + +> **NOTE**: [AngularFire has a new tree-shakable API](../../../README.md#developer-guide), you're looking at the documentation for the compatability version of the library. [See the v7 upgrade guide for more information on this change.](../../version-7-upgrade.md). + +### API: + +```ts +class AngularFireAnalytics { + updateConfig(options: {[key:string]: any}): Promise
Please login.
+ +
+traceUntil(
+ name: string,
+ test: (T) => Boolean,
+ options?: { orComplete?: true }
+)
+
+
+traceWhile(
+ name: string,
+ test: (T) => Boolean,
+ options?: { orComplete?: true }
+)
+
+{{ item.payload.key }}
+ {{ meta | async }}`
+})
+export class AppComponent {
+ meta: Observable
+
+
+AngularFire ❱ Developer Guide ❱ Realtime Database
+
+
+# Realtime Database
+
+Store and sync data with our NoSQL cloud database. Data is synced across all clients in realtime, and remains available when your app goes offline.
+
+The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps with our iOS, Android, and JavaScript SDKs, all of your clients share one Realtime Database instance and automatically receive updates with the newest data. [Learn more about the Realtime Database](https://firebase.google.com/docs/database).
+
+## Dependency Injection
+
+As a prerequisite, ensure that `AngularFire` has been added to your project via
+```bash
+ng add @angular/fire
+```
+
+Provide a Database instance in the application's `app.config.ts`:
+
+```ts
+import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
+import { provideDatabase, getDatabase } from '@angular/fire/database';
+
+export const appConfig: ApplicationConfig = {
+ providers: [
+ provideFirebaseApp(() => initializeApp({ ... })),
+ provideDatabase(() => getDatabase()),
+ ...
+ ],
+ ...
+})
+```
+
+Next inject `Database` into your component:
+
+```ts
+import { Component, inject } from '@angular/core';
+import { Database } from '@angular/fire/database';
+
+@Component({...})
+export class DepartmentComponent {
+ private database = inject(Database);
+ ...
+}
+```
+
+## Firebase API
+
+AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API.
+
+Just change your imports from `import { ... } from 'firebase/database'` to `import { ... } from '@angular/fire/database'` and follow the official documentation.
+
+[Getting Started](https://firebase.google.com/docs/database/web/start) | [API Reference](https://firebase.google.com/docs/reference/js/database)
+
+## Convenience observables
+
+AngularFire provides observables to allow convenient use of the Realtime Database with RXJS.
+
+### `object`
+
+The `object()` function creates an observable that emits object changes.
+
+| | |
+|-----------------|------------------------------------------|
+| **function** | `object(ref)` |
+| **params** | ref: `Reference` |
+| **return** | `Observable
+
+
+AngularFire ❱ Developer Guide ❱ Cloud Firestore
+
+
+# Cloud Firestore
+
+Cloud Firestore is a flexible, scalable NoSQL database for mobile, web, and server development from Firebase and Google Cloud. It keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity.
+
+[Learn more](https://firebase.google.com/docs/firestore)
+
+Cloud Firestore is the API that gives your application access to your database in the cloud or locally in your [emulator](https://firebase.google.com/docs/emulator-suite).
+
+## Dependency Injection
+
+As a prerequisite, ensure that `AngularFire` has been added to your project via
+```bash
+ng add @angular/fire
+```
+Provide a Firestore instance in the application's `app.config.ts`:
+
+```ts
+import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
+import { provideFirestore, getFirestore } from '@angular/fire/firestore';
+
+export const appConfig: ApplicationConfig = {
+ providers: [
+ provideFirebaseApp(() => initializeApp({ ... })),
+ provideFirestore(() => getFirestore()),
+ ...
+ ],
+ ...
+}
+```
+
+Next inject `Firestore` into your component:
+
+```typescript
+import { Component, inject } from '@angular/core';
+import { Firestore } from '@angular/fire/firestore';
+
+@Component({ ... })
+export class UserProfileComponent {
+ private firestore = inject(Firestore);
+ ...
+}
+```
+
+## Firebase API
+
+AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API.
+
+Update the imports from `import { ... } from 'firebase/firestore'` to `import { ... } from '@angular/fire/firestore'` and follow the official documentation.
+
+[Getting Started](https://firebase.google.com/docs/firestore/quickstart#web-modular-api) | [API Reference](https://firebase.google.com/docs/reference/js/firestore)
+
+### Reading data
+
+In Cloud Firestore data is stored in `documents` and `documents` are stored in `collections`. The path to data follows `