Skip to content

Commit 14ef140

Browse files
committed
fix(angular): added checks for invalid SSR store and pproviders
1 parent 3fc8fc3 commit 14ef140

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

examples/angular/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@angular/common": "^20.2.4",
1919
"@angular/compiler": "^20.2.4",
2020
"@angular/core": "^20.2.4",
21-
"@angular/fire": "^19.0.0",
21+
"@angular/fire": "^20.0.0",
2222
"@angular/forms": "^20.2.4",
2323
"@angular/platform-browser": "^20.2.4",
2424
"@angular/platform-browser-dynamic": "^20.2.4",
@@ -29,6 +29,7 @@
2929
"@firebase-ui/core": "workspace:*",
3030
"@firebase-ui/styles": "workspace:*",
3131
"@firebase-ui/translations": "workspace:*",
32+
"@tanstack/angular-form": "^1.1.0",
3233
"@tailwindcss/postcss": "^4.0.6",
3334
"express": "^4.18.2",
3435
"postcss": "^8.5.2",
@@ -41,7 +42,6 @@
4142
"@angular/build": "^20.2.2",
4243
"@angular/cli": "^20.2.2",
4344
"@angular/compiler-cli": "^20.2.4",
44-
"@tanstack/angular-form": "^0.42.0",
4545
"@types/express": "^4.17.17",
4646
"@types/jasmine": "~5.1.0",
4747
"@types/node": "^18.18.0",

packages/firebaseui-angular/src/lib/provider.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,28 @@ type PolicyConfig = {
4545
};
4646

4747
export function provideFirebaseUI(
48-
uiFactory: (apps: FirebaseApps) => FirebaseUIType,
48+
uiFactory: (apps: FirebaseApps) => FirebaseUIType
4949
): EnvironmentProviders {
5050
const providers: Provider[] = [
5151
// TODO: This should depend on the FirebaseAuth provider via deps,
5252
// see https://github.com/angular/angularfire/blob/35e0a9859299010488852b1826e4083abe56528f/src/firestore/firestore.module.ts#L76
53-
{ provide: FIREBASE_UI_STORE, useFactory: uiFactory, deps: [FirebaseApps] },
53+
{
54+
provide: FIREBASE_UI_STORE,
55+
useFactory: () => {
56+
try {
57+
// Try to get FirebaseApps, but handle the case where it's not available (SSR)
58+
const apps = inject(FirebaseApps, { optional: true });
59+
if (!apps || apps.length === 0) {
60+
return null as any;
61+
}
62+
return uiFactory(apps);
63+
} catch (error) {
64+
// Return null for SSR when FirebaseApps is not available
65+
return null as any;
66+
}
67+
},
68+
},
69+
FirebaseUI,
5470
];
5571

5672
return makeEnvironmentProviders(providers);
@@ -59,16 +75,17 @@ export function provideFirebaseUI(
5975
export function provideFirebaseUIPolicies(factory: () => PolicyConfig) {
6076
const providers: Provider[] = [
6177
{ provide: FIREBASE_UI_POLICIES, useFactory: factory },
78+
FirebaseUIPolicies,
6279
];
6380

6481
return makeEnvironmentProviders(providers);
6582
}
6683

6784
@Injectable({
68-
providedIn: 'root',
85+
providedIn: "root",
6986
})
7087
export class FirebaseUI {
71-
private store = inject(FIREBASE_UI_STORE);
88+
private store = inject(FIREBASE_UI_STORE, { optional: true });
7289
private destroyed$: ReplaySubject<void> = new ReplaySubject(1);
7390

7491
config() {
@@ -77,14 +94,21 @@ export class FirebaseUI {
7794

7895
translation<T extends TranslationCategory>(
7996
category: T,
80-
key: TranslationKey<T>,
97+
key: TranslationKey<T>
8198
) {
8299
return this.config().pipe(
83-
map((config) => getTranslation(config, category, key)),
100+
map((config) => getTranslation(config, category, key))
84101
);
85102
}
86103

87-
useStore<T>(store: Store<T>): Observable<T> {
104+
useStore<T>(store: Store<T> | null): Observable<T> {
105+
if (!store) {
106+
// Return an observable that emits a default value for SSR when store is not available
107+
return new Observable<T>((subscriber) => {
108+
subscriber.next({} as T);
109+
subscriber.complete();
110+
});
111+
}
88112
return new Observable<T>((sub) => {
89113
sub.next(store.get());
90114
return store.subscribe((value) => sub.next(value));

0 commit comments

Comments
 (0)