@frontegg/angular 6.13.0-alpha.7274991440 → 6.13.0-alpha.7347624886

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.
@@ -6,7 +6,7 @@ import { DefaultUrlSerializer, RouterModule } from '@angular/router';
6
6
  import { initialize } from '@frontegg/js';
7
7
  import { isAuthRoute, authStoreName, subscriptionsStoreName } from '@frontegg/redux-store';
8
8
  import { BehaviorSubject, Observable } from 'rxjs';
9
- import { FronteggFrameworks, ContextHolder } from '@frontegg/rest-api';
9
+ import { FronteggFrameworks, ContextHolder, NotEntitledJustification } from '@frontegg/rest-api';
10
10
  export { ContextHolder } from '@frontegg/rest-api';
11
11
  import angularCoreVersion from '@angular/core/package.json';
12
12
  import { __awaiter } from 'tslib';
@@ -243,10 +243,72 @@ const runNgZoneIfNeeded = (ngZone, fn) => {
243
243
  }
244
244
  };
245
245
 
246
+ /**
247
+ * A service for managing user state subscription
248
+ * The service gives the ability to subscribe to user state change and get a manipulated data when the user state changes
249
+ */
250
+ class FronteggUserSubscriptionService {
251
+ constructor(fronteggAppService) {
252
+ this.fronteggAppService = fronteggAppService;
253
+ this.userStateSubject = new BehaviorSubject(undefined);
254
+ const state = this.fronteggAppService.fronteggApp.store.getState();
255
+ this.updateUserStateIfNeeded(state.auth);
256
+ // Memoized user State
257
+ this.fronteggAppService.fronteggApp.store.subscribe(() => {
258
+ const newState = this.fronteggAppService.fronteggApp.store.getState();
259
+ this.updateUserStateIfNeeded(newState.auth);
260
+ });
261
+ }
262
+ /**
263
+ * Trigger user subject change event if the user reference changes
264
+ * No need for deep equal because we already check it internally
265
+ * @param authState
266
+ */
267
+ updateUserStateIfNeeded(authState) {
268
+ const userState = authState.user;
269
+ if (this.userStateSubject.value === userState) {
270
+ return;
271
+ }
272
+ this.userStateSubject.next(userState);
273
+ }
274
+ /**
275
+ * The function gives the ability to return a manipulated data of the user state as a subscription.
276
+ *
277
+ * @param dataManipulator Manipulator function that receives user state and returns a manipulated data
278
+ * @param observer For receiving manipulated data result
279
+ * @returns a subscription to be able to unsubscribe
280
+ */
281
+ getUserManipulatorSubscription(dataManipulator, observer) {
282
+ // used for computing the end user result because we don't return the state itself, but a calculated one
283
+ const userSubject = new BehaviorSubject(undefined);
284
+ const stateSubscription = this.userStateSubject.subscribe(user => {
285
+ userSubject.next(dataManipulator(user));
286
+ });
287
+ // subscribing the consumer observer
288
+ const userResultSubscription = userSubject.asObservable().subscribe(observer);
289
+ // monkey patched to manage both un-subscriptions: state and user manipulated result
290
+ const originalUnsubscribe = userResultSubscription.unsubscribe.bind(userResultSubscription);
291
+ userResultSubscription.unsubscribe = () => {
292
+ originalUnsubscribe();
293
+ stateSubscription.unsubscribe();
294
+ };
295
+ return userResultSubscription;
296
+ }
297
+ }
298
+ FronteggUserSubscriptionService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggUserSubscriptionService, deps: [{ token: FronteggAppService }], target: i0.ɵɵFactoryTarget.Injectable });
299
+ FronteggUserSubscriptionService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggUserSubscriptionService, providedIn: 'root' });
300
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggUserSubscriptionService, decorators: [{
301
+ type: Injectable,
302
+ args: [{
303
+ providedIn: 'root',
304
+ }]
305
+ }], ctorParameters: function () { return [{ type: FronteggAppService }]; } });
306
+
246
307
  class FronteggAuthService {
247
- constructor(fronteggAppService, router) {
308
+ constructor(fronteggAppService, router, fronteggUserSubscriptionService) {
248
309
  this.fronteggAppService = fronteggAppService;
249
310
  this.router = router;
311
+ this.fronteggUserSubscriptionService = fronteggUserSubscriptionService;
250
312
  this.authStateSubject = new BehaviorSubject({ isAuthenticated: false, isLoading: true });
251
313
  this.acceptInvitationStateSubject = new BehaviorSubject({});
252
314
  this.accountSettingsStateSubject = new BehaviorSubject({});
@@ -269,6 +331,11 @@ class FronteggAuthService {
269
331
  this.isLoadingSubject = new BehaviorSubject(true);
270
332
  this.isSSOAuthSubject = new BehaviorSubject(false);
271
333
  this.ssoACSSubject = new BehaviorSubject('');
334
+ /**
335
+ * Triggers step up flow
336
+ * @param options.maxAge optional max age
337
+ */
338
+ this.stepUp = (options) => this.fronteggAppService.fronteggApp.stepUp(options);
272
339
  // Root Actions
273
340
  this.setState = (state) => this.dispatchAction('setState', state);
274
341
  this.resetState = () => this.dispatchAction('resetState');
@@ -277,11 +344,16 @@ class FronteggAuthService {
277
344
  this.setLoginState = (state) => this.dispatchAction('setLoginState', state);
278
345
  this.resetLoginState = () => this.dispatchAction('resetLoginState');
279
346
  this.requestAuthorize = (firstTime) => this.dispatchAction('requestAuthorize', firstTime);
280
- this.loginWithRedirect = (params, shouldRedirectToLogin = true, firstTime = false, loginDirectAction) => {
347
+ this.loginWithRedirect = (params, shouldRedirectToLogin, firstTime, loginDirectAction) => {
281
348
  if (this.isHostedLoginCallbackRoute()) {
282
349
  return;
283
350
  }
284
- this.dispatchAction('requestHostedLoginAuthorizeV2', { additionalParams: params, shouldRedirectToLogin, firstTime, loginDirectAction });
351
+ this.dispatchAction('requestHostedLoginAuthorizeV2', {
352
+ additionalParams: params,
353
+ shouldRedirectToLogin: shouldRedirectToLogin !== null && shouldRedirectToLogin !== void 0 ? shouldRedirectToLogin : true,
354
+ firstTime: firstTime !== null && firstTime !== void 0 ? firstTime : false,
355
+ loginDirectAction,
356
+ });
285
357
  this.setState({ isLoading: true });
286
358
  };
287
359
  this.preLogin = (payload) => this.dispatchAction('preLogin', payload);
@@ -542,15 +614,22 @@ class FronteggAuthService {
542
614
  const hostedLoginRedirectUrl = this.fronteggAppService.authRoutes.hostedLoginRedirectUrl;
543
615
  return path.startsWith(hostedLoginRedirectUrl !== null && hostedLoginRedirectUrl !== void 0 ? hostedLoginRedirectUrl : '/oauth/callback');
544
616
  }
617
+ /**
618
+ * @param options.maxAge optional max age
619
+ * @returns A subscription for step up state - true when user is stepped up, false otherwise
620
+ */
621
+ isSteppedUp$(observer, options) {
622
+ return this.fronteggUserSubscriptionService.getUserManipulatorSubscription(() => { return this.fronteggAppService.fronteggApp.isSteppedUp(options); }, observer);
623
+ }
545
624
  }
546
- FronteggAuthService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggAuthService, deps: [{ token: FronteggAppService }, { token: i1.Router }], target: i0.ɵɵFactoryTarget.Injectable });
625
+ FronteggAuthService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggAuthService, deps: [{ token: FronteggAppService }, { token: i1.Router }, { token: FronteggUserSubscriptionService }], target: i0.ɵɵFactoryTarget.Injectable });
547
626
  FronteggAuthService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggAuthService, providedIn: 'root' });
548
627
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggAuthService, decorators: [{
549
628
  type: Injectable,
550
629
  args: [{
551
630
  providedIn: 'root',
552
631
  }]
553
- }], ctorParameters: function () { return [{ type: FronteggAppService }, { type: i1.Router }]; } });
632
+ }], ctorParameters: function () { return [{ type: FronteggAppService }, { type: i1.Router }, { type: FronteggUserSubscriptionService }]; } });
554
633
 
555
634
  class FronteggAuthGuard extends FronteggBaseGuard {
556
635
  constructor(fronteggAppService, fronteggAuthService, router, ngZone) {
@@ -733,51 +812,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.17", ngImpo
733
812
  * 3. Load entitlements on demand.
734
813
  */
735
814
  class FronteggEntitlementsService {
736
- constructor(fronteggAppService) {
815
+ constructor(fronteggAppService, fronteggUserSubscriptionService) {
737
816
  this.fronteggAppService = fronteggAppService;
738
- this.userStateSubject = new BehaviorSubject(undefined);
739
- const state = this.fronteggAppService.fronteggApp.store.getState();
740
- this.updateUserStateIfNeeded(state.auth);
741
- // Memoized entitlements State
742
- this.fronteggAppService.fronteggApp.store.subscribe(() => {
743
- const newState = this.fronteggAppService.fronteggApp.store.getState();
744
- this.updateUserStateIfNeeded(newState.auth);
745
- });
746
- }
747
- /**
748
- * Trigger entitlements subject change event if the entitlements reference changes
749
- * No need for deep equal because we already check it internally
750
- * @param authState
751
- */
752
- updateUserStateIfNeeded(authState) {
753
- const entitlementsState = authState.user;
754
- if (this.userStateSubject.value === entitlementsState) {
755
- return;
756
- }
757
- this.userStateSubject.next(entitlementsState);
758
- }
759
- /**
760
- * The function gives the ability to return a manipulated data of the user state as a subscription.
761
- *
762
- * @param dataManipulator Manipulator function that receives user state and returns a manipulated data
763
- * @param observer For receiving manipulated data result
764
- * @returns a subscription to be able to unsubscribe
765
- */
766
- getUserManipulatorSubscription(dataManipulator, observer) {
767
- // used for computing the entitlements result because we don't return the state itself, but a calculated one
768
- const userSubject = new BehaviorSubject(undefined);
769
- const stateSubscription = this.userStateSubject.subscribe(user => {
770
- userSubject.next(dataManipulator(user));
771
- });
772
- // subscribing the consumer observer
773
- const userResultSubscription = userSubject.asObservable().subscribe(observer);
774
- // monkey patched to manage both un-subscriptions: state and user manipulated result
775
- const originalUnsubscribe = userResultSubscription.unsubscribe.bind(userResultSubscription);
776
- userResultSubscription.unsubscribe = () => {
777
- originalUnsubscribe();
778
- stateSubscription.unsubscribe();
779
- };
780
- return userResultSubscription;
817
+ this.fronteggUserSubscriptionService = fronteggUserSubscriptionService;
781
818
  }
782
819
  /**
783
820
  * @param feature
@@ -788,7 +825,13 @@ class FronteggEntitlementsService {
788
825
  * @throws when entitlement is not enabled via frontegg options
789
826
  */
790
827
  featureEntitlements$(feature, observer, customAttributes) {
791
- return this.getUserManipulatorSubscription(() => { return this.fronteggAppService.fronteggApp.getFeatureEntitlements(feature, customAttributes); }, observer);
828
+ return this.fronteggUserSubscriptionService.getUserManipulatorSubscription((user) => {
829
+ // the entitlemenets-common npm doesn't know to overcome the case of signed out user, then we get console errors
830
+ if (user) {
831
+ return this.fronteggAppService.fronteggApp.getFeatureEntitlements(feature, customAttributes);
832
+ }
833
+ return { isEntitled: false, justification: NotEntitledJustification.MISSING_FEATURE };
834
+ }, observer);
792
835
  }
793
836
  /**
794
837
  * @param permission
@@ -798,7 +841,12 @@ class FronteggEntitlementsService {
798
841
  * @returns a subscription to be able to unsubscribe
799
842
  */
800
843
  permissionEntitlements$(permission, observer, customAttributes) {
801
- return this.getUserManipulatorSubscription(() => this.fronteggAppService.fronteggApp.getPermissionEntitlements(permission, customAttributes), observer);
844
+ return this.fronteggUserSubscriptionService.getUserManipulatorSubscription((user) => {
845
+ if (user) {
846
+ return this.fronteggAppService.fronteggApp.getPermissionEntitlements(permission, customAttributes);
847
+ }
848
+ return { isEntitled: false, justification: NotEntitledJustification.MISSING_PERMISSION };
849
+ }, observer);
802
850
  }
803
851
  /**
804
852
  * @param options permissionKey or featureKey in an options object
@@ -808,7 +856,13 @@ class FronteggEntitlementsService {
808
856
  * @returns a subscription to be able to unsubscribe
809
857
  */
810
858
  entitlements$(options, observer, customAttributes) {
811
- return this.getUserManipulatorSubscription(() => this.fronteggAppService.fronteggApp.getEntitlements(options, customAttributes), observer);
859
+ return this.fronteggUserSubscriptionService.getUserManipulatorSubscription((user) => {
860
+ if (user) {
861
+ return this.fronteggAppService.fronteggApp.getEntitlements(options, customAttributes);
862
+ }
863
+ const justification = 'featureKey' in options ? NotEntitledJustification.MISSING_FEATURE : NotEntitledJustification.MISSING_PERMISSION;
864
+ return { isEntitled: false, justification };
865
+ }, observer);
812
866
  }
813
867
  /**
814
868
  * Load entitlements data on demand
@@ -818,14 +872,14 @@ class FronteggEntitlementsService {
818
872
  this.fronteggAppService.fronteggApp.loadEntitlements(callback);
819
873
  }
820
874
  }
821
- FronteggEntitlementsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggEntitlementsService, deps: [{ token: FronteggAppService }], target: i0.ɵɵFactoryTarget.Injectable });
875
+ FronteggEntitlementsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggEntitlementsService, deps: [{ token: FronteggAppService }, { token: FronteggUserSubscriptionService }], target: i0.ɵɵFactoryTarget.Injectable });
822
876
  FronteggEntitlementsService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggEntitlementsService, providedIn: 'root' });
823
877
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: FronteggEntitlementsService, decorators: [{
824
878
  type: Injectable,
825
879
  args: [{
826
880
  providedIn: 'root',
827
881
  }]
828
- }], ctorParameters: function () { return [{ type: FronteggAppService }]; } });
882
+ }], ctorParameters: function () { return [{ type: FronteggAppService }, { type: FronteggUserSubscriptionService }]; } });
829
883
 
830
884
  class FronteggAppModule {
831
885
  static forRoot(config) {
@@ -838,6 +892,7 @@ class FronteggAppModule {
838
892
  FronteggAuthService,
839
893
  FronteggEntitlementsService,
840
894
  FronteggSubscriptionService,
895
+ FronteggUserSubscriptionService,
841
896
  {
842
897
  provide: FronteggAppOptionsClass,
843
898
  useValue: config,