@angular/fire 7.5.0 → 7.6.0-canary.60943d6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -72
- package/bundles/angular-fire.umd.js +1 -1
- package/bundles/angular-fire.umd.js.map +1 -1
- package/compat/firestore/interfaces.d.ts +4 -4
- package/docs/analytics.md +67 -0
- package/docs/app-check.md +53 -0
- package/docs/auth.md +165 -0
- package/docs/{analytics → compat/analytics}/getting-started.md +2 -0
- package/docs/{auth → compat/auth}/getting-started.md +2 -0
- package/docs/{auth → compat/auth}/router-guards.md +2 -0
- package/docs/{emulators → compat/emulators}/emulators.md +2 -0
- package/docs/{firestore → compat/firestore}/collections.md +2 -0
- package/docs/{firestore → compat/firestore}/documents.md +2 -0
- package/docs/{firestore → compat/firestore}/offline-data.md +2 -0
- package/docs/{firestore → compat/firestore}/querying-collections.md +2 -0
- package/docs/{functions → compat/functions}/functions.md +2 -0
- package/docs/{messaging → compat/messaging}/messaging.md +2 -0
- package/docs/{performance → compat/performance}/getting-started.md +2 -0
- package/docs/{remote-config → compat/remote-config}/getting-started.md +2 -0
- package/docs/{rtdb → compat/rtdb}/lists.md +2 -0
- package/docs/{rtdb → compat/rtdb}/objects.md +2 -0
- package/docs/{rtdb → compat/rtdb}/querying-lists.md +2 -0
- package/docs/{storage → compat/storage}/storage.md +2 -0
- package/docs/compat.md +70 -0
- package/docs/database.md +175 -0
- package/docs/firestore.md +148 -0
- package/docs/functions.md +52 -0
- package/docs/images/analytics-illo_1x.png +0 -0
- package/docs/images/auth-illo_1x.png +0 -0
- package/docs/images/cloud-messaging-illo_1x.png +0 -0
- package/docs/images/database-illo_1x.png +0 -0
- package/docs/images/firestore-illo_1x.png +0 -0
- package/docs/images/functions-illo_1x.png +0 -0
- package/docs/images/hosting-illo_1x.png +0 -0
- package/docs/images/performance-illo_1x.png +0 -0
- package/docs/images/reCAPTCHA-logo@1x.png +0 -0
- package/docs/images/remote-config-illo_1x.png +0 -0
- package/docs/images/storage-illo_1x.png +0 -0
- package/docs/install-and-setup.md +39 -86
- package/docs/install-firebase-tools.md +78 -0
- package/docs/messaging.md +25 -0
- package/docs/performance.md +57 -0
- package/docs/remote-config.md +53 -0
- package/docs/storage.md +90 -0
- package/docs/universal/cloud-functions.md +27 -6
- package/docs/universal/getting-started.md +9 -134
- package/esm2015/compat/firestore/interfaces.js +1 -1
- package/esm2015/core.js +1 -1
- package/fesm2015/angular-fire.js +1 -1
- package/fesm2015/angular-fire.js.map +1 -1
- package/package.json +9 -10
- package/schematics/deploy/actions.js +8 -3
- package/schematics/deploy/builder.js +2 -2
- package/schematics/firebaseTools.js +4 -2
- package/schematics/setup/index.js +58 -29
- package/schematics/setup/prompts.js +18 -46
- package/schematics/utils.js +32 -28
- package/docs/ionic/authentication.md +0 -104
- package/docs/ionic/cli.md +0 -218
- package/docs/ionic/v2.md +0 -531
- package/docs/ionic/v3.md +0 -705
- package/schematics/setup/ssr.js +0 -100
- package/schematics/setup/static.js +0 -78
|
@@ -3,6 +3,21 @@
|
|
|
3
3
|
### 1. Create a new project
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
+
# Using yarn create
|
|
7
|
+
yarn create @angular <project-name>
|
|
8
|
+
cd <project-name>
|
|
9
|
+
```
|
|
10
|
+
or
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# Using npm create
|
|
14
|
+
npm create @angular <project-name>
|
|
15
|
+
cd <project-name>
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
optionally installing the tooling directly:
|
|
19
|
+
```bash
|
|
20
|
+
# Installing the tooling directly
|
|
6
21
|
npm install -g @angular/cli
|
|
7
22
|
ng new <project-name>
|
|
8
23
|
cd <project-name>
|
|
@@ -16,85 +31,18 @@ The Angular CLI's `new` command will set up the latest Angular build in a new pr
|
|
|
16
31
|
ng add @angular/fire
|
|
17
32
|
```
|
|
18
33
|
|
|
19
|
-
Now that you have a new project setup, install AngularFire and Firebase from npm.
|
|
20
|
-
|
|
21
|
-
### 3. Add Firebase config to environments variable
|
|
22
|
-
|
|
23
|
-
Open `/src/environments/environment.ts` and add your Firebase configuration. You can find your project configuration in [the Firebase Console](https://console.firebase.google.com). Click the Gear icon next to Project Overview, in the Your Apps section, create a new app and choose the type Web. Give the app a name and copy the config values provided.
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
export const environment = {
|
|
27
|
-
production: false,
|
|
28
|
-
firebase: {
|
|
29
|
-
apiKey: '<your-key>',
|
|
30
|
-
authDomain: '<your-project-authdomain>',
|
|
31
|
-
databaseURL: '<your-database-URL>',
|
|
32
|
-
projectId: '<your-project-id>',
|
|
33
|
-
storageBucket: '<your-storage-bucket>',
|
|
34
|
-
messagingSenderId: '<your-messaging-sender-id>',
|
|
35
|
-
appId: '<your-app-id>',
|
|
36
|
-
measurementId: '<your-measurement-id>'
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### 4. Setup `@NgModule` for the `AngularFireModule`
|
|
42
|
-
|
|
43
|
-
Open `/src/app/app.module.ts`, inject the Firebase providers, and specify your Firebase configuration.
|
|
44
|
-
|
|
45
|
-
```ts
|
|
46
|
-
import { BrowserModule } from '@angular/platform-browser';
|
|
47
|
-
import { NgModule } from '@angular/core';
|
|
48
|
-
import { AppComponent } from './app.component';
|
|
49
|
-
import { AngularFireModule } from '@angular/fire/compat';
|
|
50
|
-
import { environment } from '../environments/environment';
|
|
51
|
-
|
|
52
|
-
@NgModule({
|
|
53
|
-
imports: [
|
|
54
|
-
BrowserModule,
|
|
55
|
-
AngularFireModule.initializeApp(environment.firebase)
|
|
56
|
-
],
|
|
57
|
-
declarations: [ AppComponent ],
|
|
58
|
-
bootstrap: [ AppComponent ]
|
|
59
|
-
})
|
|
60
|
-
export class AppModule {}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### 5. Setup individual `@NgModule`s
|
|
34
|
+
Now that you have a new project setup, install AngularFire and Firebase from npm. This will complete the following tasks:
|
|
64
35
|
|
|
65
|
-
|
|
36
|
+
1. Add Firebase config to environments variables
|
|
37
|
+
2. Configure `@NgModule` for the `AngularFireModule`
|
|
66
38
|
|
|
67
|
-
|
|
39
|
+
### 3. Inject `Firestore`
|
|
68
40
|
|
|
69
|
-
|
|
70
|
-
import { BrowserModule } from '@angular/platform-browser';
|
|
71
|
-
import { NgModule } from '@angular/core';
|
|
72
|
-
import { AppComponent } from './app.component';
|
|
73
|
-
import { AngularFireModule } from '@angular/fire/compat';
|
|
74
|
-
import { AngularFireAnalyticsModule } from '@angular/fire/compat/analytics';
|
|
75
|
-
import { AngularFirestoreModule } from '@angular/fire/compat/firestore';
|
|
76
|
-
import { environment } from '../environments/environment';
|
|
77
|
-
|
|
78
|
-
@NgModule({
|
|
79
|
-
imports: [
|
|
80
|
-
BrowserModule,
|
|
81
|
-
AngularFireModule.initializeApp(environment.firebase),
|
|
82
|
-
AngularFireAnalyticsModule,
|
|
83
|
-
AngularFirestoreModule
|
|
84
|
-
],
|
|
85
|
-
declarations: [ AppComponent ],
|
|
86
|
-
bootstrap: [ AppComponent ]
|
|
87
|
-
})
|
|
88
|
-
export class AppModule {}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### 7. Inject `AngularFirestore`
|
|
92
|
-
|
|
93
|
-
Open `/src/app/app.component.ts`, and make sure to modify/delete any tests to get the sample working (tests are still important, you know):
|
|
41
|
+
Open `/src/app/app.component.ts`, and make the following changes to :
|
|
94
42
|
|
|
95
43
|
```ts
|
|
96
|
-
import { Component } from '@angular/core';
|
|
97
|
-
import {
|
|
44
|
+
import { Component, inject } from '@angular/core';
|
|
45
|
+
import { Firestore } from '@angular/fire/firestore';
|
|
98
46
|
|
|
99
47
|
@Component({
|
|
100
48
|
selector: 'app-root',
|
|
@@ -102,19 +50,21 @@ import { AngularFirestore } from '@angular/fire/compat/firestore';
|
|
|
102
50
|
styleUrls: ['app.component.css']
|
|
103
51
|
})
|
|
104
52
|
export class AppComponent {
|
|
105
|
-
|
|
53
|
+
firestore: Firestore = inject(Firestore);
|
|
54
|
+
|
|
55
|
+
constructor() {
|
|
106
56
|
|
|
107
57
|
}
|
|
108
58
|
}
|
|
109
59
|
```
|
|
110
60
|
|
|
111
|
-
###
|
|
61
|
+
### 4. Bind a Firestore collection to a list
|
|
112
62
|
|
|
113
63
|
In `/src/app/app.component.ts`:
|
|
114
64
|
|
|
115
65
|
```ts
|
|
116
|
-
import { Component } from '@angular/core';
|
|
117
|
-
import {
|
|
66
|
+
import { Component, inject } from '@angular/core';
|
|
67
|
+
import { Firestore, collection, collectionData } from '@angular/fire/firestore';
|
|
118
68
|
import { Observable } from 'rxjs';
|
|
119
69
|
|
|
120
70
|
@Component({
|
|
@@ -123,9 +73,12 @@ import { Observable } from 'rxjs';
|
|
|
123
73
|
styleUrls: ['app.component.css']
|
|
124
74
|
})
|
|
125
75
|
export class AppComponent {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
76
|
+
firestore: Firestore = inject(Firestore)
|
|
77
|
+
items$: Observable<any[]>;
|
|
78
|
+
|
|
79
|
+
constructor() {
|
|
80
|
+
const aCollection = collection(this.firestore, 'items')
|
|
81
|
+
this.items$ = collectionData(aCollection);
|
|
129
82
|
}
|
|
130
83
|
}
|
|
131
84
|
```
|
|
@@ -134,25 +87,25 @@ Open `/src/app/app.component.html`:
|
|
|
134
87
|
|
|
135
88
|
```html
|
|
136
89
|
<ul>
|
|
137
|
-
<li class="text" *ngFor="let item of items | async">
|
|
90
|
+
<li class="text" *ngFor="let item of items$ | async">
|
|
138
91
|
{{item.name}}
|
|
139
92
|
</li>
|
|
140
93
|
</ul>
|
|
141
94
|
```
|
|
142
95
|
|
|
143
|
-
###
|
|
96
|
+
### 5. Run your app locally
|
|
144
97
|
|
|
145
98
|
```bash
|
|
146
99
|
ng serve
|
|
147
100
|
```
|
|
148
101
|
|
|
149
|
-
Your Angular app will compile and serve locally, visit it we should
|
|
102
|
+
Your Angular app will compile and serve locally, visit it we should find an empty list.
|
|
150
103
|
|
|
151
104
|
In another tab [start adding data to an `items` collection in Firestore](https://firebase.google.com/docs/firestore/manage-data/add-data). *As we're not authenticating users yet, be sure to start Firestore in **test mode** or allow reading from the `items` collection in Security Rules (`allow read: if true`).*
|
|
152
105
|
|
|
153
|
-
Once you've created a `items` collection and are inserting documents, you should
|
|
106
|
+
Once you've created a `items` collection and are inserting documents, you should find data streaming into your Angular application and being rendered in your browser.
|
|
154
107
|
|
|
155
|
-
###
|
|
108
|
+
### 6. Deploy your app
|
|
156
109
|
|
|
157
110
|
Finally, we can deploy the application to Firebase hosting:
|
|
158
111
|
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Firebase Tools Install and Setup
|
|
2
|
+
|
|
3
|
+
### 1. Install package
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install -g firebase-tools
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
or with `yarn`
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
yarn global add firebase-tools
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
You can also choose to install it as a dependency for your project rather than globally
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install --save-dev firebase-tools
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
or with `yarn`
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add -D firebase-tools
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 2. Configure Firebase Tools
|
|
28
|
+
|
|
29
|
+
In your projects root directory run:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
firebase init
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
or if your installed it within your project rather than globally
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npx firebase init
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
or with `yarn`
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
yarn firebase init
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This will ask you to login if you are not logged in already, the process will take you through a browser
|
|
48
|
+
redirect to log you into Firebase.
|
|
49
|
+
|
|
50
|
+
### 3. Choose what Firebase features
|
|
51
|
+
|
|
52
|
+
`firebase-tools` displays Firebase features you want to configure.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
? Which Firebase features do you want to set up for this directory? Press Space
|
|
56
|
+
to select features, then Enter to confirm your choices. (Press <space> to select
|
|
57
|
+
, <a> to toggle all, <i> to invert selection, and <enter> to proceed)
|
|
58
|
+
◯ Realtime Database: Configure a security rules file for Realtime Database and
|
|
59
|
+
(optionally) provision default instance
|
|
60
|
+
◯ Firestore: Configure security rules and indexes files for Firestore
|
|
61
|
+
◯ Functions: Configure a Cloud Functions directory and its files
|
|
62
|
+
◯ Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub
|
|
63
|
+
Action deploys
|
|
64
|
+
◯ Hosting: Set up GitHub Action deploys
|
|
65
|
+
(Move up and down to reveal more choices)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 4. Connect your repo to a Firebase project
|
|
69
|
+
|
|
70
|
+
The CLI will then walk you through making sure your repo is configured with a Firebase project.
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
? Please select an option:
|
|
74
|
+
◯ Use an existing project
|
|
75
|
+
◯ Create a new project
|
|
76
|
+
◯ Add Firebase to an existing Google Cloud Platform project
|
|
77
|
+
◯ Don't set up a default project
|
|
78
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<img align="right" width="30%" src="images/cloud-messaging-illo_1x.png">
|
|
2
|
+
|
|
3
|
+
<small>
|
|
4
|
+
<a href="https://github.com/angular/angularfire">AngularFire</a> ❱ <a href="../README.md#developer-guide">Developer Guide</a> ❱ Realtime Cloud Messaging
|
|
5
|
+
</small>
|
|
6
|
+
|
|
7
|
+
# Cloud Messaging
|
|
8
|
+
|
|
9
|
+
The FCM JavaScript API lets you receive notification messages in web apps running in browsers that support the Push API.
|
|
10
|
+
|
|
11
|
+
[Learn More](https://firebase.google.com/docs/cloud-messaging/)
|
|
12
|
+
|
|
13
|
+
## Dependency Injection
|
|
14
|
+
|
|
15
|
+
YADA YADA YADA
|
|
16
|
+
|
|
17
|
+
## Firebase API
|
|
18
|
+
|
|
19
|
+
Something something look at the offical docs
|
|
20
|
+
|
|
21
|
+
## Convenience observables
|
|
22
|
+
|
|
23
|
+
### Foo
|
|
24
|
+
|
|
25
|
+
bar baz
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<img align="right" width="30%" src="images/performance-illo_1x.png">
|
|
2
|
+
|
|
3
|
+
<small>
|
|
4
|
+
<a href="https://github.com/angular/angularfire">AngularFire</a> ❱ <a href="../README.md#developer-guide">Developer Guide</a> ❱ Realtime Performance Monitoring
|
|
5
|
+
</small>
|
|
6
|
+
|
|
7
|
+
# Performance Monitoring
|
|
8
|
+
|
|
9
|
+
Firebase Performance Monitoring is a service that helps you to gain insight into the performance characteristics of your Apple, Android, and web apps.
|
|
10
|
+
|
|
11
|
+
[Learn More](https://firebase.google.com/docs/perf-mon)
|
|
12
|
+
|
|
13
|
+
## Dependency Injection
|
|
14
|
+
|
|
15
|
+
As a prerequisite, ensure that `AngularFire` has been added to your project via
|
|
16
|
+
```bash
|
|
17
|
+
ng add @angular/fire
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Provide a Performance instance and configuration in the application's `NgModule` (`app.module.ts`):
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
|
|
24
|
+
import { getPerformance, providePerformance} from '@angular/fire/performance';
|
|
25
|
+
|
|
26
|
+
@NgModule({
|
|
27
|
+
imports: [
|
|
28
|
+
provideFirebaseApp(() => initializeApp(environment.firebase)),
|
|
29
|
+
providePerformance(() => getPerformance()),
|
|
30
|
+
]
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Next inject it into your component:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { Component, inject} from '@angular/core';
|
|
38
|
+
import { Performance } from '@angular/fire/performance';
|
|
39
|
+
|
|
40
|
+
@Component({ ... })
|
|
41
|
+
export class PerformanceComponent {
|
|
42
|
+
private performance: Performance = inject(Performance);
|
|
43
|
+
...
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Firebase API
|
|
48
|
+
|
|
49
|
+
The [Performance documentation](https://firebase.google.com/docs/reference/js/performance.md#performance_package) is available on the Firebase website.
|
|
50
|
+
|
|
51
|
+
## Services
|
|
52
|
+
|
|
53
|
+
Coming soon.
|
|
54
|
+
|
|
55
|
+
## Convenience observables
|
|
56
|
+
|
|
57
|
+
Coming soon.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
<img align="right" width="30%" src="images/remote-config-illo_1x.png">
|
|
2
|
+
|
|
3
|
+
<small>
|
|
4
|
+
<a href="https://github.com/angular/angularfire">AngularFire</a> ❱ <a href="../README.md#developer-guide">Developer Guide</a> ❱ Realtime Remote Config
|
|
5
|
+
</small>
|
|
6
|
+
|
|
7
|
+
# Remote Config
|
|
8
|
+
|
|
9
|
+
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.
|
|
10
|
+
|
|
11
|
+
[Learn More](https://firebase.google.com/docs/remote-config/)
|
|
12
|
+
|
|
13
|
+
## Dependency Injection
|
|
14
|
+
|
|
15
|
+
As a prerequisite, ensure that `AngularFire` has been added to your project via
|
|
16
|
+
```bash
|
|
17
|
+
ng add @angular/fire
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Provide a Performance instance and configuration in the application's `NgModule` (`app.module.ts`):
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
|
|
24
|
+
import { getRemoteConfig, provideRemoteConfig} from '@angular/fire/remote-config';
|
|
25
|
+
|
|
26
|
+
@NgModule({
|
|
27
|
+
imports: [
|
|
28
|
+
provideFirebaseApp(() => initializeApp(environment.firebase)),
|
|
29
|
+
provideRemoteConfig(() => getRemoteConfig()),
|
|
30
|
+
]
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Next inject it into your component:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { Component, inject} from '@angular/core';
|
|
38
|
+
import { RemoteConfig } from '@angular/fire/remote-config';
|
|
39
|
+
|
|
40
|
+
@Component({ ... })
|
|
41
|
+
export class RemoteConfigComponent {
|
|
42
|
+
private remoteConfig: RemoteConfig = inject(RemoteConfig);
|
|
43
|
+
...
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Firebase API
|
|
48
|
+
|
|
49
|
+
The [Remote Config documentation](https://firebase.google.com/docs/reference/js/remote-config) is available on the Firebase website.
|
|
50
|
+
|
|
51
|
+
## Convenience observables
|
|
52
|
+
|
|
53
|
+
Coming soon.
|
package/docs/storage.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
<img align="right" width="30%" src="images/storage-illo_1x.png">
|
|
2
|
+
|
|
3
|
+
<small>
|
|
4
|
+
<a href="https://github.com/angular/angularfire">AngularFire</a> ❱ <a href="../README.md#developer-guide">Developer Guide</a> ❱ Realtime Cloud Storage
|
|
5
|
+
</small>
|
|
6
|
+
|
|
7
|
+
# Cloud Storage
|
|
8
|
+
|
|
9
|
+
Cloud Storage allows developers to upload and share user generated content such as images, video and more. Data is stored in Google Cloud Storage.
|
|
10
|
+
|
|
11
|
+
[Learn more](https://firebase.google.com/docs/storage)
|
|
12
|
+
|
|
13
|
+
## Dependency Injection
|
|
14
|
+
|
|
15
|
+
AngularFire allows you to work with Firebase Storage via Angular's Dependency Injection.
|
|
16
|
+
|
|
17
|
+
As a prerequisite, ensure that `AngularFire` has been added to your project via
|
|
18
|
+
```bash
|
|
19
|
+
ng add @angular/fire
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Provide a Firebase Storage instance in the application's `NgModule` (`app.module.ts`):
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
|
|
26
|
+
import { getStorage, provideStorage } from '@angular/fire/storage';
|
|
27
|
+
|
|
28
|
+
@NgModule({
|
|
29
|
+
imports: [
|
|
30
|
+
provideFirebaseApp(() => initializeApp(environment.firebase)),
|
|
31
|
+
provideStorage(() => getStorage()),
|
|
32
|
+
]
|
|
33
|
+
})
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Next inject it into your component:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { Component, inject} from '@angular/core';
|
|
40
|
+
import { Storage } from '@angular/fire/storage';
|
|
41
|
+
|
|
42
|
+
@Component({ ... })
|
|
43
|
+
export class StorageComponent {
|
|
44
|
+
private storage: Storage = inject(Storage);
|
|
45
|
+
...
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## Firebase API
|
|
51
|
+
|
|
52
|
+
AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API.
|
|
53
|
+
|
|
54
|
+
Update the imports from `import { ... } from 'firebase/storage'` to `import { ... } from '@angular/fire/storage'` and follow the offical documentation.
|
|
55
|
+
|
|
56
|
+
[Getting Started](https://firebase.google.com/docs/storage/web/start) | [API Reference](https://firebase.google.com/docs/reference/js/storage)
|
|
57
|
+
|
|
58
|
+
## File Upload Example
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { Component, inject } from '@angular/core';
|
|
62
|
+
import { Storage, ref, uploadBytesResumable } from '@angular/fire/storage';
|
|
63
|
+
|
|
64
|
+
@Component({
|
|
65
|
+
selector: 'app-storage',
|
|
66
|
+
template: `
|
|
67
|
+
<h1>Storage</h1>
|
|
68
|
+
<label for="fileUpload">Choose a File</label>
|
|
69
|
+
<input id="fileUpload" type="file" #upload>
|
|
70
|
+
<button (click)="uploadFile(upload)">Upload</button>
|
|
71
|
+
`,
|
|
72
|
+
})
|
|
73
|
+
export class StorageComponent {
|
|
74
|
+
private readonly storage: Storage = inject(Storage);
|
|
75
|
+
|
|
76
|
+
uploadFile(input: HTMLInputElement) {
|
|
77
|
+
if (!input.files) return
|
|
78
|
+
|
|
79
|
+
const files: FileList = input.files;
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < files.length; i++) {
|
|
82
|
+
const file = files.item(i);
|
|
83
|
+
if (file) {
|
|
84
|
+
const storageRef = ref(this.storage, file.name);
|
|
85
|
+
uploadBytesResumable(storageRef, file);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
@@ -40,8 +40,8 @@ Let's go ahead and modify your `package.json` to build for Cloud Functions:
|
|
|
40
40
|
```js
|
|
41
41
|
"scripts": {
|
|
42
42
|
// ... omitted
|
|
43
|
-
"build": "ng build && npm run
|
|
44
|
-
"copy:hosting": "cp -r ./dist/YOUR_PROJECT_NAME/* ./public && rm ./public/index.html",
|
|
43
|
+
"build": "ng build && npm run build:ssr && npm run copy:hosting && npm run build:functions",
|
|
44
|
+
"copy:hosting": "cp -r ./dist/YOUR_PROJECT_NAME/* ./public && rm -f ./public/index.html",
|
|
45
45
|
"build:functions": "npm run --prefix functions build"
|
|
46
46
|
},
|
|
47
47
|
```
|
|
@@ -51,21 +51,42 @@ Change the build script in your `functions/package.json` to the following:
|
|
|
51
51
|
```js
|
|
52
52
|
"scripts": {
|
|
53
53
|
// ... omitted
|
|
54
|
-
"build": "rm -
|
|
54
|
+
"build": "rm -rf ./dist && cp -r ../dist . && tsc",
|
|
55
55
|
}
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
+
You will either need to install the dependencies from the `functions/package.json` or add them to your root `package.json`.
|
|
59
|
+
|
|
58
60
|
Finally, add the following to your `functions/src/index.ts`:
|
|
59
61
|
|
|
60
62
|
```ts
|
|
61
63
|
export const universal = functions.https.onRequest((request, response) => {
|
|
62
|
-
require(`${process.cwd()}/dist/YOUR_PROJECT_NAME
|
|
64
|
+
require(`${process.cwd()}/dist/YOUR_PROJECT_NAME/server/main`).app()(request, response);
|
|
63
65
|
});
|
|
64
66
|
```
|
|
65
67
|
|
|
66
|
-
|
|
68
|
+
In the `server.ts` file generated by the Angular CLI ensure that the `index.html` is being read from the correct place.
|
|
69
|
+
|
|
70
|
+
As of writing the Angular CLI generates:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index.html';
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
this needs to be changed to:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : join(distFolder, 'index.html');
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
So that the `index.html` is read from the correct `dist` folder.
|
|
83
|
+
|
|
84
|
+
You should now be able to run `npm run build` to build your project for Firebase Hosting and Cloud Functions.
|
|
85
|
+
|
|
86
|
+
To test, spin up the emulator with `firebase serve`. Once you've confirmed its working go ahead and `firebase deploy`.
|
|
67
87
|
|
|
68
|
-
|
|
88
|
+
_Note: Universal will now SSR your application and serve it, depending on how you have your deployment setup some assets may fail to
|
|
89
|
+
serve because the Cloud Function is at a different route than the `APP_BASE_REF` configured._
|
|
69
90
|
|
|
70
91
|
### [Next Step: Prerendering your Universal application](prerendering.md)
|
|
71
92
|
|