Skip to content

Commit 1064042

Browse files
authored
FirebaseServerApp documentation (#3529)
1 parent ffd6672 commit 1064042

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/auth.md

+33
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,39 @@ Update the imports from `import { ... } from 'firebase/auth'` to `import { ... }
5757

5858
[Getting Started](https://firebase.google.com/docs/auth/web/start) | [API Reference](https://firebase.google.com/docs/reference/js/auth)
5959

60+
## Server-side Rendering
61+
62+
To support Auth context in server-side rendering, you can provide `FirebaseServerApp`:
63+
64+
```ts
65+
import { ApplicationConfig, PLATFORM_ID, inject } from '@angular/core';
66+
import { isPlatformBrowser } from '@angular/common';
67+
import { provideFirebaseApp, initializeApp, initializeServeApp, initializeServerApp } from '@angular/fire/app';
68+
import { provideAuth, getAuth } from '@angular/fire/auth';
69+
70+
export const appConfig: ApplicationConfig = {
71+
providers: [
72+
provideFirebaseApp(() => {
73+
if (isPlatformBrowser(inject(PLATFORM_ID))) {
74+
return initializeApp(firebaseConfig);
75+
}
76+
// Optional, since it's null in dev-mode and SSG
77+
const request = inject(REQUEST, { optional: true });
78+
const authIdToken = request?.headers.authorization?.split("Bearer ")[1];
79+
return initializeServerApp(firebaseConfig, {
80+
authIdToken,
81+
releaseOnDeref: request || undefined
82+
});
83+
}),
84+
provideAuth(() => getAuth(inject(FirebaseApp)),
85+
...
86+
],
87+
...
88+
})
89+
```
90+
91+
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.__
92+
6093
## Convenience observables
6194
6295
AngularFire provides observables to allow convenient use of the Firebase Authentication with RXJS.

0 commit comments

Comments
 (0)