@tech4good/angular-schematics 21.4.0 → 21.6.0
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/CHANGELOG.md +33 -0
- package/package.json +1 -1
- package/src/entity/files/__name@dasherize__/__name@dasherize__.mock.ts.template +2 -1
- package/src/entity/files/__name@dasherize__/__name@dasherize__.model.ts.template +2 -1
- package/src/entity/index.js +57 -0
- package/src/entity/index.js.map +1 -1
- package/src/entity/schema.json +4 -0
- package/src/ng-add/files/.firebaserc.template +2 -1
- package/src/ng-add/files/.gitignore.template +2 -0
- package/src/ng-add/files/bigquery/queries/active_time_by_route.sql.template +34 -0
- package/src/ng-add/files/bigquery/scripts/export_entities.py.template +143 -0
- package/src/ng-add/files/bigquery/scripts/run_query.py.template +167 -0
- package/src/ng-add/files/bigquery/views/events_flat.sql.template +60 -0
- package/src/ng-add/files/bigquery/views/logs_latest.sql.template +34 -0
- package/src/ng-add/files/docs-infra/analytics-guide.md.template +170 -0
- package/src/ng-add/files/docs-infra/authentication-architecture.md.template +270 -0
- package/src/ng-add/files/docs-infra/infrastructure-guide.md.template +16 -10
- package/src/ng-add/files/firebase.json.template +6 -0
- package/src/ng-add/files/firebase.logs.json.template +5 -0
- package/src/ng-add/files/firestore.indexes.json.template +112 -0
- package/src/ng-add/files/firestore.logs.rules.spec.ts.template +100 -0
- package/src/ng-add/files/firestore.logs.rules.template +8 -0
- package/src/ng-add/files/firestore.rules.spec.ts.template +84 -15
- package/src/ng-add/files/firestore.rules.template +29 -17
- package/src/ng-add/files/functions/.gitignore.template +3 -0
- package/src/ng-add/files/functions/package.json.template +12 -0
- package/src/ng-add/files/functions/src/index.ts.template +44 -0
- package/src/ng-add/files/functions/tsconfig.json.template +13 -0
- package/src/ng-add/files/jest.config.ts.template +1 -1
- package/src/ng-add/files/package.json.template +7 -4
- package/src/ng-add/files/src/app/admin/admin-panel/admin-panel.component.html.template +3 -1
- package/src/ng-add/files/src/app/admin/admin-panel/admin-panel.component.spec.ts.template +6 -8
- package/src/ng-add/files/src/app/admin/admin-panel/admin-panel.component.ts.template +2 -0
- package/src/ng-add/files/src/app/app.component.html.template +2 -2
- package/src/ng-add/files/src/app/app.component.ts.template +2 -0
- package/src/ng-add/files/src/app/app.routes.ts.template +6 -0
- package/src/ng-add/files/src/app/auth-routing.spec.ts.template +9 -4
- package/src/ng-add/files/src/app/core/analytics/log.service.ts.template +26 -4
- package/src/ng-add/files/src/app/core/analytics/time-analytics.service.spec.ts.template +145 -2
- package/src/ng-add/files/src/app/core/analytics/time-analytics.service.ts.template +355 -149
- package/src/ng-add/files/src/app/core/auth/auth.service.ts.template +6 -1
- package/src/ng-add/files/src/app/core/auth/guards/require-admin.guard.spec.ts.template +38 -0
- package/src/ng-add/files/src/app/core/auth/guards/require-admin.guard.ts.template +3 -2
- package/src/ng-add/files/src/app/core/auth/guards/require-auth-and-consent.guard.ts.template +10 -3
- package/src/ng-add/files/src/app/core/firebase/cached-listeners.service.ts.template +11 -5
- package/src/ng-add/files/src/app/core/firebase/database.service.ts.template +34 -17
- package/src/ng-add/files/src/app/core/firebase/email-link-context.service.spec.ts.template +59 -0
- package/src/ng-add/files/src/app/core/firebase/email-link-context.service.ts.template +147 -0
- package/src/ng-add/files/src/app/core/firebase/firebase.mock.service.ts.template +104 -17
- package/src/ng-add/files/src/app/core/firebase/firebase.service.spec.ts.template +116 -0
- package/src/ng-add/files/src/app/core/firebase/firebase.service.ts.template +367 -70
- package/src/ng-add/files/src/app/core/store/app.store.ts.template +4 -1
- package/src/ng-add/files/src/app/core/store/auth/auth.mock.ts.template +52 -66
- package/src/ng-add/files/src/app/core/store/auth/auth.store.spec.ts.template +127 -0
- package/src/ng-add/files/src/app/core/store/auth/auth.store.ts.template +216 -147
- package/src/ng-add/files/src/app/core/store/user/user.mock.ts.template +2 -4
- package/src/ng-add/files/src/app/core/store/user/user.model.ts.template +1 -1
- package/src/ng-add/files/src/app/core/store/user-private/user-private.model.ts.template +1 -0
- package/src/ng-add/files/src/app/first-time/auth-action/auth-action.component.html.template +4 -0
- package/src/ng-add/files/src/app/first-time/auth-action/auth-action.component.scss.template +1 -0
- package/src/ng-add/files/src/app/first-time/auth-action/auth-action.component.ts.template +38 -0
- package/src/ng-add/files/src/app/first-time/consent/consent.component.html.template +172 -48
- package/src/ng-add/files/src/app/first-time/consent/consent.component.scss.template +31 -0
- package/src/ng-add/files/src/app/first-time/consent/consent.component.spec.ts.template +31 -3
- package/src/ng-add/files/src/app/first-time/consent/consent.component.ts.template +12 -11
- package/src/ng-add/files/src/app/first-time/consent/consent.guard.ts.template +5 -7
- package/src/ng-add/files/src/app/first-time/early-access/early-access.component.spec.ts.template +12 -1
- package/src/ng-add/files/src/app/first-time/early-access/early-access.component.ts.template +93 -48
- package/src/ng-add/files/src/app/first-time/early-access/early-access.guard.ts.template +12 -2
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.html.template +18 -0
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.scss.template +2 -0
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.spec.ts.template +40 -0
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.ts.template +76 -0
- package/src/ng-add/files/src/app/first-time/first-time-routing.spec.ts.template +14 -8
- package/src/ng-add/files/src/app/first-time/landing/hero/hero.component.html.template +19 -7
- package/src/ng-add/files/src/app/first-time/landing/hero/hero.component.scss.template +51 -0
- package/src/ng-add/files/src/app/first-time/landing/hero/hero.component.ts.template +29 -5
- package/src/ng-add/files/src/app/first-time/landing/landing.component.html.template +2 -1
- package/src/ng-add/files/src/app/first-time/landing/landing.component.scss.template +12 -8
- package/src/ng-add/files/src/app/first-time/landing/landing.component.spec.ts.template +31 -10
- package/src/ng-add/files/src/app/first-time/landing/landing.component.ts.template +6 -19
- package/src/ng-add/files/src/app/first-time/login/login.component.html.template +58 -6
- package/src/ng-add/files/src/app/first-time/login/login.component.scss.template +151 -22
- package/src/ng-add/files/src/app/first-time/login/login.component.spec.ts.template +18 -38
- package/src/ng-add/files/src/app/first-time/login/login.component.ts.template +59 -31
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.html.template +12 -0
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.scss.template +39 -0
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.spec.ts.template +68 -0
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.ts.template +20 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.html.template +24 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.scss.template +50 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.spec.ts.template +65 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.ts.template +59 -0
- package/src/ng-add/files/src/app/main/account-profile/guest-deletion-dialog.component.ts.template +51 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.html.template +50 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.scss.template +137 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.spec.ts.template +57 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.ts.template +59 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.html.template +133 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.scss.template +207 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.spec.ts.template +74 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.ts.template +130 -0
- package/src/ng-add/files/src/app/main/home/home.component.ts.template +0 -5
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.html.template +15 -0
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.scss.template +79 -0
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.spec.ts.template +59 -0
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.ts.template +44 -0
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.html.template +45 -15
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.scss.template +103 -19
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.spec.ts.template +73 -9
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.ts.template +46 -18
- package/src/ng-add/files/src/environments/environment.prod.ts.template +1 -0
- package/src/ng-add/files/src/environments/environment.test.ts.template +1 -0
- package/src/ng-add/files/src/environments/environment.ts.template +1 -1
- package/src/ng-add/index_spec.d.ts +1 -0
- package/src/ng-add/index_spec.js +108 -0
- package/src/ng-add/index_spec.js.map +1 -0
- package/tsconfig.json +2 -2
|
@@ -1,92 +1,78 @@
|
|
|
1
|
-
import { signalStore, patchState, withState, withMethods, withComputed } from '@ngrx/signals';
|
|
2
1
|
import { computed, inject, Injector } from '@angular/core';
|
|
2
|
+
import { patchState, signalStore, withComputed, withMethods, withState } from '@ngrx/signals';
|
|
3
|
+
import { environment } from '../../../../environments/environment';
|
|
3
4
|
import { UserMockDB, USER_DB } from '../user/user.mock';
|
|
4
5
|
import { USERPRIVATE_DB } from '../user-private/user-private.mock';
|
|
5
|
-
import { environment } from '../../../../environments/environment';
|
|
6
|
-
import { User } from '../user/user.model';
|
|
7
6
|
|
|
8
7
|
const savedUser = environment.cacheMockData ? localStorage.getItem('mockUser') : null;
|
|
9
8
|
const initialUser = savedUser ? JSON.parse(savedUser) : null;
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const afUser = store.afUser();
|
|
27
|
-
if (!afUser) return null;
|
|
28
|
-
const userStore = injector.get(UserMockDB);
|
|
29
|
-
return userStore.selectEntity(afUser.uid) || USER_DB.find((u) => u.__id === afUser.uid) || null;
|
|
30
|
-
}),
|
|
31
|
-
};
|
|
32
|
-
}),
|
|
33
|
-
withMethods((store) => ({
|
|
34
|
-
login() {
|
|
35
|
-
const afUser = {
|
|
36
|
-
uid: USER_DB[0].__id,
|
|
37
|
-
email: USERPRIVATE_DB.find((u) => u.__id === USER_DB[0].__id)?.email || '',
|
|
38
|
-
displayName: USER_DB[0].name,
|
|
39
|
-
photoURL: USER_DB[0].photoURL || '',
|
|
40
|
-
};
|
|
41
|
-
if (environment.cacheMockData) {
|
|
42
|
-
localStorage.setItem('mockUser', JSON.stringify(afUser));
|
|
43
|
-
}
|
|
44
|
-
patchState(store, { afUser });
|
|
45
|
-
},
|
|
46
|
-
logout() {
|
|
47
|
-
if (environment.cacheMockData) {
|
|
48
|
-
localStorage.removeItem('mockUser');
|
|
49
|
-
}
|
|
50
|
-
patchState(store, { afUser: null });
|
|
51
|
-
},
|
|
52
|
-
})),
|
|
53
|
-
);
|
|
10
|
+
const createSession = (overrides: Record<string, unknown> = {}) => {
|
|
11
|
+
const isAnon = overrides['isAnonymous'] === true;
|
|
12
|
+
const uid = isAnon ? 'guest-anon-id' : USER_DB[0].__id;
|
|
13
|
+
return {
|
|
14
|
+
uid,
|
|
15
|
+
email: isAnon ? null : USERPRIVATE_DB.find((user) => user.__id === uid)?.email || '',
|
|
16
|
+
displayName: isAnon ? 'Guest' : USER_DB[0].name,
|
|
17
|
+
photoURL: isAnon ? null : USER_DB[0].photoURL || null,
|
|
18
|
+
isAnonymous: isAnon,
|
|
19
|
+
emailVerified: !isAnon,
|
|
20
|
+
emailLinkEnabled: false,
|
|
21
|
+
providerIds: isAnon ? [] : ['google.com'],
|
|
22
|
+
...overrides,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
54
25
|
|
|
55
|
-
|
|
26
|
+
const createAuthMock = (initialAfUser: ReturnType<typeof createSession> | null) => signalStore(
|
|
56
27
|
{ providedIn: 'root' },
|
|
57
|
-
withState({
|
|
58
|
-
afUser: {
|
|
59
|
-
uid: USER_DB[0].__id,
|
|
60
|
-
email: USERPRIVATE_DB.find((u) => u.__id === USER_DB[0].__id)?.email || '',
|
|
61
|
-
displayName: USER_DB[0].name,
|
|
62
|
-
photoURL: USER_DB[0].photoURL || '',
|
|
63
|
-
} as { uid: string; email: string; displayName: string; photoURL: string } | null,
|
|
64
|
-
isLoadingLogin: false,
|
|
65
|
-
}),
|
|
28
|
+
withState({ afUser: initialAfUser, isLoadingLogin: false }),
|
|
66
29
|
withComputed((store) => {
|
|
67
30
|
const injector = inject(Injector);
|
|
68
31
|
return {
|
|
69
32
|
user: computed(() => {
|
|
70
|
-
const afUser = store
|
|
33
|
+
const afUser = store['afUser']();
|
|
71
34
|
if (!afUser) return null;
|
|
72
35
|
const userStore = injector.get(UserMockDB);
|
|
73
|
-
return userStore.selectEntity(afUser.uid) || USER_DB.find((
|
|
36
|
+
return userStore.selectEntity(afUser.uid) || USER_DB.find((user) => user.__id === afUser.uid) || null;
|
|
74
37
|
}),
|
|
75
38
|
};
|
|
76
39
|
}),
|
|
77
40
|
withMethods((store) => ({
|
|
78
|
-
|
|
41
|
+
loadAuth() {},
|
|
42
|
+
async signInWithGoogle() {
|
|
43
|
+
patchState(store, { afUser: createSession() });
|
|
44
|
+
},
|
|
45
|
+
async loginAnonymously() {
|
|
79
46
|
patchState(store, {
|
|
80
|
-
afUser: {
|
|
81
|
-
uid: USER_DB[0].__id,
|
|
82
|
-
email: USERPRIVATE_DB.find((u) => u.__id === USER_DB[0].__id)?.email || '',
|
|
83
|
-
displayName: USER_DB[0].name,
|
|
84
|
-
photoURL: USER_DB[0].photoURL || '',
|
|
85
|
-
},
|
|
47
|
+
afUser: createSession({ email: null, displayName: 'Guest', photoURL: null, isAnonymous: true, emailVerified: false, providerIds: [] }),
|
|
86
48
|
});
|
|
87
49
|
},
|
|
88
|
-
|
|
50
|
+
async linkGoogle() {
|
|
51
|
+
patchState(store, { afUser: createSession({ providerIds: ['google.com'] }) });
|
|
52
|
+
},
|
|
53
|
+
async sendEmailSignInLink() {
|
|
54
|
+
return;
|
|
55
|
+
},
|
|
56
|
+
async sendEmailLinkForCurrentAccount() {
|
|
57
|
+
return;
|
|
58
|
+
},
|
|
59
|
+
getStoredEmailForEmailLink() {
|
|
60
|
+
return null;
|
|
61
|
+
},
|
|
62
|
+
async completeEmailSignInLink() {
|
|
63
|
+
const session = createSession({ emailLinkEnabled: true, providerIds: ['password'] });
|
|
64
|
+
patchState(store, { afUser: session });
|
|
65
|
+
return session;
|
|
66
|
+
},
|
|
67
|
+
async deleteAnonymousAccount() {
|
|
68
|
+
patchState(store, { afUser: null });
|
|
69
|
+
},
|
|
70
|
+
async logout() {
|
|
71
|
+
if (environment.cacheMockData) localStorage.removeItem('mockUser');
|
|
89
72
|
patchState(store, { afUser: null });
|
|
90
73
|
},
|
|
91
74
|
})),
|
|
92
75
|
);
|
|
76
|
+
|
|
77
|
+
export const AuthMockDB = createAuthMock(initialUser ? createSession(initialUser) : null);
|
|
78
|
+
export const AuthLoggedInMockDB = createAuthMock(createSession());
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { Router } from '@angular/router';
|
|
3
|
+
import { BehaviorSubject } from 'rxjs';
|
|
4
|
+
import { CachedListenersService } from '../../firebase/cached-listeners.service';
|
|
5
|
+
import {
|
|
6
|
+
AuthSession,
|
|
7
|
+
DATABASE_SERVICE,
|
|
8
|
+
DatabaseService,
|
|
9
|
+
} from '../../firebase/database.service';
|
|
10
|
+
import { UserContextStore } from '../user-context/user-context.store';
|
|
11
|
+
import { UserStore } from '../user/user.store';
|
|
12
|
+
import { AuthStore } from './auth.store';
|
|
13
|
+
|
|
14
|
+
describe('AuthStore session teardown', () => {
|
|
15
|
+
it('waits for refresh provisioning before tearing down stores on logout', async () => {
|
|
16
|
+
const session: AuthSession = {
|
|
17
|
+
uid: 'refresh-user',
|
|
18
|
+
email: 'user@example.com',
|
|
19
|
+
displayName: 'Test User',
|
|
20
|
+
photoURL: null,
|
|
21
|
+
isAnonymous: false,
|
|
22
|
+
emailVerified: true,
|
|
23
|
+
emailLinkEnabled: false,
|
|
24
|
+
providerIds: ['google.com'],
|
|
25
|
+
};
|
|
26
|
+
const authSession = new BehaviorSubject<AuthSession | null>(null);
|
|
27
|
+
let finishProvisioning!: () => void;
|
|
28
|
+
const provisioning = new Promise<void>((resolve) => {
|
|
29
|
+
finishProvisioning = resolve;
|
|
30
|
+
});
|
|
31
|
+
const db = {
|
|
32
|
+
afUser: jest.fn().mockReturnValue(authSession),
|
|
33
|
+
ensureUserProfile: jest.fn().mockReturnValue(provisioning),
|
|
34
|
+
logout: jest.fn().mockResolvedValue(undefined),
|
|
35
|
+
} as unknown as DatabaseService;
|
|
36
|
+
const userStore = {
|
|
37
|
+
selectEntity: jest.fn().mockReturnValue(null),
|
|
38
|
+
load: jest.fn().mockResolvedValue(undefined),
|
|
39
|
+
stream: jest.fn(),
|
|
40
|
+
reset: jest.fn(),
|
|
41
|
+
};
|
|
42
|
+
const resetOnlyStore = { reset: jest.fn() };
|
|
43
|
+
const cachedListeners = { teardownAllStreams: jest.fn() };
|
|
44
|
+
|
|
45
|
+
TestBed.configureTestingModule({
|
|
46
|
+
providers: [
|
|
47
|
+
AuthStore,
|
|
48
|
+
{ provide: DATABASE_SERVICE, useValue: db },
|
|
49
|
+
{ provide: UserStore, useValue: userStore },
|
|
50
|
+
{ provide: UserContextStore, useValue: resetOnlyStore },
|
|
51
|
+
{
|
|
52
|
+
provide: Router,
|
|
53
|
+
useValue: {
|
|
54
|
+
navigate: jest.fn().mockResolvedValue(true),
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{ provide: CachedListenersService, useValue: cachedListeners },
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
const authStore = TestBed.inject(AuthStore);
|
|
61
|
+
authStore.loadAuth();
|
|
62
|
+
authSession.next(session);
|
|
63
|
+
await Promise.resolve();
|
|
64
|
+
expect(db.ensureUserProfile).toHaveBeenCalledWith(session);
|
|
65
|
+
|
|
66
|
+
const logout = authStore.logout({ doNotRoute: true });
|
|
67
|
+
await Promise.resolve();
|
|
68
|
+
expect(cachedListeners.teardownAllStreams).not.toHaveBeenCalled();
|
|
69
|
+
expect(db.logout).not.toHaveBeenCalled();
|
|
70
|
+
|
|
71
|
+
finishProvisioning();
|
|
72
|
+
await logout;
|
|
73
|
+
|
|
74
|
+
expect(userStore.stream).toHaveBeenCalledTimes(1);
|
|
75
|
+
expect(cachedListeners.teardownAllStreams).toHaveBeenCalledTimes(1);
|
|
76
|
+
expect(db.logout).toHaveBeenCalledTimes(1);
|
|
77
|
+
expect(authStore.afUser()).toBeNull();
|
|
78
|
+
expect(userStore.stream.mock.invocationCallOrder[0]).toBeLessThan(
|
|
79
|
+
cachedListeners.teardownAllStreams.mock.invocationCallOrder[0],
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('sets isLoadingLogin to true during completeEmailSignInLink', async () => {
|
|
84
|
+
const session: AuthSession = {
|
|
85
|
+
uid: 'new-email-user',
|
|
86
|
+
email: 'new@example.com',
|
|
87
|
+
displayName: null,
|
|
88
|
+
photoURL: null,
|
|
89
|
+
isAnonymous: false,
|
|
90
|
+
emailVerified: true,
|
|
91
|
+
emailLinkEnabled: true,
|
|
92
|
+
providerIds: ['password'],
|
|
93
|
+
};
|
|
94
|
+
let isLoadingDuringCall = false;
|
|
95
|
+
const db = {
|
|
96
|
+
afUser: jest.fn().mockReturnValue(new BehaviorSubject<AuthSession | null>(null)),
|
|
97
|
+
completeEmailSignInLink: jest.fn().mockImplementation(async () => {
|
|
98
|
+
isLoadingDuringCall = authStore.isLoadingLogin();
|
|
99
|
+
return session;
|
|
100
|
+
}),
|
|
101
|
+
ensureUserProfile: jest.fn().mockResolvedValue(undefined),
|
|
102
|
+
} as unknown as DatabaseService;
|
|
103
|
+
const userStore = {
|
|
104
|
+
selectEntity: jest.fn().mockReturnValue(null),
|
|
105
|
+
load: jest.fn().mockResolvedValue(undefined),
|
|
106
|
+
stream: jest.fn(),
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
TestBed.configureTestingModule({
|
|
110
|
+
providers: [
|
|
111
|
+
AuthStore,
|
|
112
|
+
{ provide: DATABASE_SERVICE, useValue: db },
|
|
113
|
+
{ provide: UserStore, useValue: userStore },
|
|
114
|
+
{ provide: UserContextStore, useValue: { reset: jest.fn() } },
|
|
115
|
+
{ provide: Router, useValue: { navigate: jest.fn().mockResolvedValue(true) } },
|
|
116
|
+
{ provide: CachedListenersService, useValue: { teardownAllStreams: jest.fn() } },
|
|
117
|
+
],
|
|
118
|
+
});
|
|
119
|
+
const authStore = TestBed.inject(AuthStore);
|
|
120
|
+
|
|
121
|
+
expect(authStore.isLoadingLogin()).toBe(false);
|
|
122
|
+
await authStore.completeEmailSignInLink('https://example.com/link', 'new@example.com');
|
|
123
|
+
expect(isLoadingDuringCall).toBe(true);
|
|
124
|
+
expect(authStore.isLoadingLogin()).toBe(false);
|
|
125
|
+
expect(authStore.afUser()).toEqual(session);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Injector, computed, inject } from '@angular/core';
|
|
2
2
|
import { signalStore, patchState, withState, withMethods, withComputed } from '@ngrx/signals';
|
|
3
3
|
import { rxMethod } from '@ngrx/signals/rxjs-interop';
|
|
4
|
-
import { User
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import { User } from '../user/user.model';
|
|
5
|
+
import { AuthSession, DATABASE_SERVICE } from '../../firebase/database.service';
|
|
6
|
+
import { CachedListenersService } from '../../firebase/cached-listeners.service';
|
|
7
|
+
import { EMPTY, of, pipe, from, firstValueFrom } from 'rxjs';
|
|
8
|
+
import { tap, catchError, switchMap } from 'rxjs/operators';
|
|
8
9
|
import { Router } from '@angular/router';
|
|
10
|
+
import { MatDialog } from '@angular/material/dialog';
|
|
9
11
|
import { UserStore } from '../user/user.store';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
+
import { UserContextStore } from '../user-context/user-context.store';
|
|
13
|
+
import { GuestDeletionDialogComponent } from 'src/app/main/account-profile/guest-deletion-dialog.component';
|
|
12
14
|
|
|
13
15
|
type State = {
|
|
14
|
-
afUser:
|
|
16
|
+
afUser: AuthSession | null;
|
|
15
17
|
isLoadingLogin: boolean;
|
|
16
|
-
}
|
|
18
|
+
};
|
|
17
19
|
|
|
18
20
|
const initialState: State = {
|
|
19
21
|
afUser: null,
|
|
@@ -30,158 +32,225 @@ export const AuthStore = signalStore(
|
|
|
30
32
|
return userStore.selectEntity(afUser.uid) || null;
|
|
31
33
|
}),
|
|
32
34
|
})),
|
|
33
|
-
withMethods(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}),
|
|
45
|
-
)),
|
|
35
|
+
withMethods(
|
|
36
|
+
(
|
|
37
|
+
store,
|
|
38
|
+
db = inject(DATABASE_SERVICE),
|
|
39
|
+
router = inject(Router),
|
|
40
|
+
userStore = inject(UserStore),
|
|
41
|
+
injector = inject(Injector),
|
|
42
|
+
cachedListeners = inject(CachedListenersService),
|
|
43
|
+
) => {
|
|
44
|
+
const refreshProvisioning = new Set<Promise<void>>();
|
|
45
|
+
let isEndingSession = false;
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Provisions a user profile in Firestore (if it doesn't exist yet),
|
|
49
|
+
* loads it into the UserStore, and starts a real-time stream for updates.
|
|
50
|
+
*
|
|
51
|
+
* This is extracted as a shared helper so that both direct login methods
|
|
52
|
+
* (loginAnonymously, signInWithGoogle, etc.) and the reactive loadAuth
|
|
53
|
+
* pipeline can reuse the same logic without duplication.
|
|
54
|
+
*/
|
|
55
|
+
async function provisionAndLoadUser(session: AuthSession): Promise<void> {
|
|
56
|
+
await db.ensureUserProfile(session);
|
|
57
|
+
await userStore.load([['__id', '==', session.uid]], {});
|
|
58
|
+
userStore.stream([['__id', '==', session.uid]], {}, injector);
|
|
59
|
+
}
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
function trackRefreshProvisioning(session: AuthSession): Promise<void> {
|
|
62
|
+
const provisioning = provisionAndLoadUser(session);
|
|
63
|
+
refreshProvisioning.add(provisioning);
|
|
64
|
+
void provisioning.then(
|
|
65
|
+
() => refreshProvisioning.delete(provisioning),
|
|
66
|
+
() => refreshProvisioning.delete(provisioning),
|
|
67
|
+
);
|
|
68
|
+
return provisioning;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function awaitRefreshProvisioning(): Promise<void> {
|
|
72
|
+
while (refreshProvisioning.size) {
|
|
73
|
+
await Promise.allSettled([...refreshProvisioning]);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
loadAuth: rxMethod<void>(
|
|
79
|
+
pipe(
|
|
80
|
+
// 1. Listen for changes in the authenticated Firebase user.
|
|
81
|
+
// This emits immediately if a user is already signed in (from local cache/cookie),
|
|
82
|
+
// or emits whenever they log in or out.
|
|
83
|
+
switchMap(() =>
|
|
84
|
+
db.afUser().pipe(
|
|
63
85
|
catchError((e) => {
|
|
64
|
-
console.error(
|
|
86
|
+
console.error(e);
|
|
65
87
|
return EMPTY;
|
|
66
88
|
}),
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
// If signed out, just pass null down.
|
|
70
|
-
return of(null);
|
|
71
|
-
}
|
|
72
|
-
}),
|
|
89
|
+
),
|
|
90
|
+
),
|
|
73
91
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
92
|
+
// 2. Provision a single profile for each new Firebase UID, then load it.
|
|
93
|
+
//
|
|
94
|
+
// IMPORTANT: This reactive path primarily handles page refresh and
|
|
95
|
+
// re-authentication scenarios where the user is already authenticated
|
|
96
|
+
// from cache. During page refresh, both Firebase Auth and Firestore are
|
|
97
|
+
// initialized from IndexedDB simultaneously, so the auth token is
|
|
98
|
+
// guaranteed to be available when Firestore operations run.
|
|
99
|
+
//
|
|
100
|
+
// For fresh logins (loginAnonymously, signInWithGoogle, etc.), the login
|
|
101
|
+
// methods provision directly after Firebase has completed the sign-in.
|
|
102
|
+
// Ignore this listener while that direct flow is in progress: Angular
|
|
103
|
+
// Fire can publish the auth-state change before Firestore has applied
|
|
104
|
+
// the new credentials, so duplicate provisioning here can be denied.
|
|
105
|
+
//
|
|
106
|
+
// This architecture avoids the race condition where Angular Fire's
|
|
107
|
+
// scheduled auth notification arrives before Firestore's internal
|
|
108
|
+
// CredentialsProvider has processed the new token, which would cause
|
|
109
|
+
// "Missing or insufficient permissions" errors on fresh logins.
|
|
110
|
+
switchMap((afUser) => {
|
|
111
|
+
if (afUser) {
|
|
112
|
+
if (isEndingSession) return EMPTY;
|
|
113
|
+
if (store.isLoadingLogin()) return of(afUser);
|
|
114
|
+
return from(trackRefreshProvisioning(afUser)).pipe(
|
|
115
|
+
switchMap(() => of(afUser)),
|
|
116
|
+
catchError((e) => {
|
|
117
|
+
console.error('[AuthStore loadAuth] streamEntity error:', e);
|
|
118
|
+
return EMPTY;
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
// If signed out, just pass null down.
|
|
123
|
+
return of(null);
|
|
124
|
+
}
|
|
125
|
+
}),
|
|
81
126
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
127
|
+
// 3. Keep the Firebase session available to guards and UI.
|
|
128
|
+
tap((afUser: State['afUser']) => {
|
|
129
|
+
if (!afUser) {
|
|
130
|
+
patchState(store, { afUser: null });
|
|
131
|
+
} else {
|
|
132
|
+
patchState(store, { afUser });
|
|
133
|
+
}
|
|
134
|
+
}),
|
|
135
|
+
),
|
|
136
|
+
),
|
|
137
|
+
async signInWithGoogle(): Promise<void> {
|
|
138
|
+
patchState(store, { isLoadingLogin: true });
|
|
139
|
+
try {
|
|
140
|
+
const session = await db.signInWithGoogle();
|
|
141
|
+
// Provision user directly after auth completes, where the auth token
|
|
142
|
+
// is guaranteed to be valid and Firestore's CredentialsProvider has it.
|
|
143
|
+
await provisionAndLoadUser(session);
|
|
144
|
+
patchState(store, { afUser: session });
|
|
145
|
+
} finally {
|
|
146
|
+
patchState(store, { isLoadingLogin: false });
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
async loginAnonymously(): Promise<void> {
|
|
150
|
+
patchState(store, { isLoadingLogin: true });
|
|
151
|
+
try {
|
|
152
|
+
const session = await db.signInAnonymously();
|
|
153
|
+
// Provision user directly after anonymous auth completes, where the
|
|
154
|
+
// auth token is guaranteed to be valid. This avoids the race condition
|
|
155
|
+
// where loadAuth's reactive path fires before Firestore's internal
|
|
156
|
+
// auth state has been updated with the new anonymous token.
|
|
157
|
+
await provisionAndLoadUser(session);
|
|
158
|
+
patchState(store, { afUser: session });
|
|
159
|
+
} finally {
|
|
160
|
+
patchState(store, { isLoadingLogin: false });
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
async linkGoogle(): Promise<void> {
|
|
164
|
+
const session = await db.linkGoogle();
|
|
165
|
+
// Re-provision after linking to update the profile with Google info.
|
|
166
|
+
await provisionAndLoadUser(session);
|
|
167
|
+
patchState(store, { afUser: session });
|
|
168
|
+
},
|
|
169
|
+
async sendEmailSignInLink(email: string): Promise<void> {
|
|
170
|
+
patchState(store, { isLoadingLogin: true });
|
|
171
|
+
try {
|
|
172
|
+
await db.sendEmailSignInLink(email);
|
|
173
|
+
} finally {
|
|
174
|
+
patchState(store, { isLoadingLogin: false });
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
async sendEmailLinkForCurrentAccount(): Promise<void> {
|
|
178
|
+
await db.sendEmailLinkForCurrentAccount();
|
|
179
|
+
},
|
|
180
|
+
getStoredEmailForEmailLink(): string | null {
|
|
181
|
+
return db.getStoredEmailForEmailLink();
|
|
182
|
+
},
|
|
183
|
+
async completeEmailSignInLink(url: string, email: string): Promise<AuthSession> {
|
|
184
|
+
patchState(store, { isLoadingLogin: true });
|
|
185
|
+
try {
|
|
186
|
+
const session = await db.completeEmailSignInLink(url, email);
|
|
187
|
+
// Provision user directly after email link auth completes.
|
|
188
|
+
await provisionAndLoadUser(session);
|
|
189
|
+
patchState(store, { afUser: session });
|
|
190
|
+
return session;
|
|
191
|
+
} finally {
|
|
192
|
+
patchState(store, { isLoadingLogin: false });
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
resetAllStores(): void {
|
|
196
|
+
cachedListeners.teardownAllStreams();
|
|
197
|
+
userStore.reset();
|
|
198
|
+
const userContextStore = injector.get(UserContextStore, null);
|
|
199
|
+
if (userContextStore) userContextStore.reset();
|
|
200
|
+
},
|
|
201
|
+
async deleteAnonymousAccount(): Promise<void> {
|
|
202
|
+
isEndingSession = true;
|
|
203
|
+
try {
|
|
204
|
+
await awaitRefreshProvisioning();
|
|
205
|
+
// Tear down streams and reset in-memory store states & load-time caches
|
|
206
|
+
this.resetAllStores();
|
|
207
|
+
await db.deleteAnonymousAccount();
|
|
208
|
+
// Deleting the Firebase Auth user on the server does not immediately
|
|
209
|
+
// clear the browser's Firebase session. Clear it before navigating so
|
|
210
|
+
// the landing-page guard does not wait for the now-deleted profile.
|
|
211
|
+
await db.logout();
|
|
212
|
+
patchState(store, { afUser: null });
|
|
213
|
+
await router.navigate(['/landing']);
|
|
214
|
+
} finally {
|
|
215
|
+
isEndingSession = false;
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
async logout(params?: { doNotRoute: boolean }): Promise<void> {
|
|
219
|
+
try {
|
|
220
|
+
const session = store.afUser();
|
|
221
|
+
if (session?.isAnonymous) {
|
|
222
|
+
const dialog = injector.get(MatDialog, null);
|
|
223
|
+
if (dialog) {
|
|
224
|
+
const result = await firstValueFrom(dialog.open(GuestDeletionDialogComponent).afterClosed());
|
|
225
|
+
if (result === 'save') {
|
|
226
|
+
await router.navigate(['/account']);
|
|
227
|
+
return;
|
|
100
228
|
}
|
|
229
|
+
if (result !== true) return;
|
|
101
230
|
}
|
|
231
|
+
await this.deleteAnonymousAccount();
|
|
232
|
+
return;
|
|
102
233
|
}
|
|
103
|
-
}),
|
|
104
|
-
)),
|
|
105
|
-
async login(provider: string, params: {
|
|
106
|
-
scope?: string,
|
|
107
|
-
doNotRoute: boolean
|
|
108
|
-
} = { doNotRoute: false }): Promise<void> {
|
|
109
|
-
patchState(store, { isLoadingLogin: true });
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
const loginResult = await db.login(provider, params.scope);
|
|
113
|
-
const afUser = loginResult.user;
|
|
114
|
-
patchState(store, { afUser });
|
|
115
|
-
|
|
116
|
-
const dbUser: User = await db.getEntity('users', afUser.uid);
|
|
117
|
-
|
|
118
|
-
const navLocation = ['/home'];
|
|
119
|
-
// update the user if there is already a dbUser
|
|
120
|
-
if (dbUser) {
|
|
121
|
-
const changes = {
|
|
122
|
-
name: afUser.displayName || afUser.email,
|
|
123
|
-
photoURL: afUser.photoURL || null,
|
|
124
|
-
};
|
|
125
|
-
try {
|
|
126
|
-
await userStore.update(dbUser.__id, changes);
|
|
127
|
-
await db.addEntity('userPrivates', {
|
|
128
|
-
__id: dbUser.__id,
|
|
129
|
-
email: afUser.email,
|
|
130
|
-
});
|
|
131
234
|
|
|
132
|
-
|
|
133
|
-
router.navigate(navLocation, { queryParamsHandling: 'preserve' });
|
|
134
|
-
}
|
|
135
|
-
} catch (e) {
|
|
136
|
-
console.error(e);
|
|
137
|
-
throw e;
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
const newUser = {
|
|
141
|
-
__id: afUser.uid,
|
|
142
|
-
name: afUser.displayName || afUser.email,
|
|
143
|
-
photoURL: afUser.photoURL || null,
|
|
144
|
-
isAdmin: false,
|
|
145
|
-
consented: false,
|
|
146
|
-
accessState: AccessState.SUBMIT_INTEREST,
|
|
147
|
-
};
|
|
235
|
+
isEndingSession = true;
|
|
148
236
|
try {
|
|
149
|
-
await
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
154
|
-
|
|
237
|
+
await awaitRefreshProvisioning();
|
|
238
|
+
// Tear down all active Firestore streams and clear in-memory entity stores BEFORE signing out.
|
|
239
|
+
this.resetAllStores();
|
|
240
|
+
await db.logout();
|
|
241
|
+
patchState(store, { afUser: null });
|
|
155
242
|
if (!params || !params.doNotRoute) {
|
|
156
|
-
router.navigate(
|
|
243
|
+
router.navigate(['/landing']);
|
|
157
244
|
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
throw e;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
} catch (e) {
|
|
164
|
-
switch (e.code) {
|
|
165
|
-
case 'auth/account-exists-with-different-credential': {
|
|
166
|
-
db.loginLink(e);
|
|
245
|
+
} finally {
|
|
246
|
+
isEndingSession = false;
|
|
167
247
|
}
|
|
248
|
+
} catch (e) {
|
|
249
|
+
console.error(e);
|
|
250
|
+
throw e;
|
|
168
251
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
},
|
|
174
|
-
async logout(params?: { doNotRoute: boolean }): Promise<void> {
|
|
175
|
-
try {
|
|
176
|
-
await db.logout();
|
|
177
|
-
if (!params || !params.doNotRoute) {
|
|
178
|
-
router.navigate(['/landing']);
|
|
179
|
-
}
|
|
180
|
-
} catch (e) {
|
|
181
|
-
console.error(e);
|
|
182
|
-
throw e;
|
|
183
|
-
}
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
}),
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
},
|
|
255
|
+
),
|
|
187
256
|
);
|