@dereekb/dbx-firebase 7.3.0 → 7.4.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.
@@ -1,4 +1,4 @@
1
- import { urlWithoutParameters, cachedGetter, addToSet, removeFromSet, filterMaybeValues, mapIterable, asArray, excludeValuesFromArray, containsStringAnyCase, addToSetCopy, forEachKeyValue, readableError, isMaybeSo, firstValue } from '@dereekb/util';
1
+ import { urlWithoutParameters, AUTH_ADMIN_ROLE, cachedGetter, addToSet, removeFromSet, filterMaybeValues, mapIterable, asArray, excludeValuesFromArray, containsStringAnyCase, addToSetCopy, forEachKeyValue, readableError, isMaybeSo, firstValue } from '@dereekb/util';
2
2
  import * as i0 from '@angular/core';
3
3
  import { InjectionToken, Injectable, Inject, Optional, Component, Input, Directive, EventEmitter, Output, NgModule, Injector, forwardRef, Host } from '@angular/core';
4
4
  import { getToken, initializeAppCheck, ReCaptchaV3Provider } from 'firebase/app-check';
@@ -185,6 +185,9 @@ const DEFAULT_DBX_FIREBASE_AUTH_SERVICE_DELEGATE = {
185
185
  },
186
186
  isOnboarded(dbxFirebaseAuthService) {
187
187
  return dbxFirebaseAuthService.authUserState$.pipe(map((x) => x === 'user'));
188
+ },
189
+ isAdminInAuthRoleSet(authRoleSet) {
190
+ return authRoleSet.has(AUTH_ADMIN_ROLE);
188
191
  }
189
192
  };
190
193
  // MARK: Service
@@ -214,6 +217,7 @@ class DbxFirebaseAuthService {
214
217
  this.authRoles$ = delegate.authRolesObs(this);
215
218
  this.isOnboarded$ = delegate.isOnboarded(this);
216
219
  this._authRoleClaimsService = delegate.authRoleClaimsService;
220
+ this.isAdminInAuthRoleSet = delegate.isAdminInAuthRoleSet;
217
221
  }
218
222
  rolesForClaims(claims) {
219
223
  let result;
@@ -227,7 +231,15 @@ class DbxFirebaseAuthService {
227
231
  return result;
228
232
  }
229
233
  getAuthContextInfo() {
230
- return firstValueFrom(this.authUser$).then((user) => (user ? new DbxFirebaseAuthContextInfo(this, user) : undefined));
234
+ return firstValueFrom(this.authUser$).then((user) => this.loadAuthContextInfoForUser(user));
235
+ }
236
+ async loadAuthContextInfoForUser(user) {
237
+ let result;
238
+ if (user) {
239
+ const jwtToken = await user.getIdTokenResult();
240
+ result = new DbxFirebaseAuthContextInfo(this, user, jwtToken);
241
+ }
242
+ return result;
231
243
  }
232
244
  logInWithGoogle() {
233
245
  return this.logInWithPopup(new GoogleAuthProvider());
@@ -282,19 +294,31 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.2", ngImpor
282
294
  * FirebaseAuthContextInfo implementation from DbxFirebaseAuthService.
283
295
  */
284
296
  class DbxFirebaseAuthContextInfo {
285
- constructor(service, user) {
297
+ constructor(service, user, jwtToken) {
286
298
  this.service = service;
287
299
  this.user = user;
300
+ this.jwtToken = jwtToken;
288
301
  this._token = cachedGetter(() => firebaseAuthTokenFromUser(this.user));
302
+ this._roles = cachedGetter(() => this.service.rolesForClaims(this.getClaims()));
303
+ this._isAdmin = cachedGetter(() => this.service.isAdminInAuthRoleSet(this._roles()));
289
304
  }
290
305
  get uid() {
291
306
  return this.user.uid;
292
307
  }
308
+ isAdmin() {
309
+ return this._isAdmin();
310
+ }
311
+ getClaims() {
312
+ return this.jwtToken.claims;
313
+ }
314
+ getAuthRoles() {
315
+ return this._roles();
316
+ }
293
317
  loadClaims() {
294
- return this.user.getIdTokenResult().then((x) => x.claims);
318
+ return Promise.resolve(this.getClaims());
295
319
  }
296
320
  loadAuthRoles() {
297
- return this.loadClaims().then((x) => this.service.rolesForClaims(x));
321
+ return Promise.resolve(this.getAuthRoles());
298
322
  }
299
323
  get token() {
300
324
  return this._token();
@@ -1401,6 +1425,7 @@ function defaultDbxFirebaseAuthServiceDelegateWithClaimsService(config) {
1401
1425
  authUserStateObs: DEFAULT_DBX_FIREBASE_AUTH_SERVICE_DELEGATE.authUserStateObs,
1402
1426
  authRolesObs: authRolesObsWithClaimsService(config),
1403
1427
  isOnboarded: DEFAULT_DBX_FIREBASE_AUTH_SERVICE_DELEGATE.isOnboarded,
1428
+ isAdminInAuthRoleSet: config.isAdminInAuthRoleSet ?? DEFAULT_DBX_FIREBASE_AUTH_SERVICE_DELEGATE.isAdminInAuthRoleSet,
1404
1429
  authRoleClaimsService: config.claimsService
1405
1430
  };
1406
1431
  }