@esolve/ng-esolve-connect 0.29.4 → 0.29.6

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.
@@ -168,6 +168,26 @@ class EsolveSessionService {
168
168
  clearTimeout(this.key_expiration_timer);
169
169
  }
170
170
  }
171
+ getCachedSession() {
172
+ let json_data = this.cookieService.get(this.storage_key);
173
+ if (json_data === '') {
174
+ const local_store_data = this.getBackup();
175
+ if (local_store_data) {
176
+ json_data = local_store_data;
177
+ }
178
+ }
179
+ const stored_session = (json_data ? JSON.parse(json_data) : null);
180
+ if (!stored_session) {
181
+ return null;
182
+ }
183
+ const expiration_date = new Date(stored_session.expiration_date);
184
+ const loaded_session = new EsolveSession(stored_session.id, stored_session.location_id, stored_session.key, expiration_date);
185
+ return loaded_session;
186
+ }
187
+ resetSessionCache() {
188
+ this.cookieService.delete(this.storage_key, '/');
189
+ this.removeBackup();
190
+ }
171
191
  restore(expirationCallback, invalidSessionCallback) {
172
192
  if (this.currentSession.valid) {
173
193
  return;
@@ -249,6 +269,15 @@ class EsolveSessionService {
249
269
  }
250
270
  localStorage.setItem(this.storage_key, data);
251
271
  }
272
+ removeBackup() {
273
+ if (!this.is_browser) {
274
+ return;
275
+ }
276
+ if (!localStorage) {
277
+ return;
278
+ }
279
+ localStorage.removeItem(this.storage_key);
280
+ }
252
281
  }
253
282
  EsolveSessionService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveSessionService, deps: [{ token: PLATFORM_ID }, { token: ESOLVE_CONNECT_CONFIG }, { token: EsolveCookieService }], target: i0.ɵɵFactoryTarget.Injectable });
254
283
  EsolveSessionService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveSessionService, providedIn: 'root' });
@@ -1712,6 +1741,11 @@ class EsolveAuthService {
1712
1741
  this.errorHandler = errorHandler;
1713
1742
  this.cookieService = cookieService;
1714
1743
  }
1744
+ static preValidate(auth) {
1745
+ return () => {
1746
+ return auth.validateCachedTokens();
1747
+ };
1748
+ }
1715
1749
  getAccessToken(email, password, anonymous = false) {
1716
1750
  let params = new HttpParams();
1717
1751
  if (anonymous) {
@@ -1740,11 +1774,14 @@ class EsolveAuthService {
1740
1774
  }));
1741
1775
  }
1742
1776
  autoLogin() {
1743
- this.session.restore(this.handleExpiration(), () => {
1744
- firstValueFrom(this.getAccessToken('', '', true)).then(() => {
1745
- // empty
1746
- }, (error) => {
1747
- console.error(error);
1777
+ return __awaiter(this, void 0, void 0, function* () {
1778
+ yield this.validateCachedTokens();
1779
+ this.session.restore(this.handleExpiration(), () => {
1780
+ firstValueFrom(this.getAccessToken('', '', true)).then(() => {
1781
+ // empty
1782
+ }, (error) => {
1783
+ console.error(error);
1784
+ });
1748
1785
  });
1749
1786
  });
1750
1787
  }
@@ -1802,6 +1839,26 @@ class EsolveAuthService {
1802
1839
  throw new Error('Invalid response');
1803
1840
  });
1804
1841
  }
1842
+ validateCachedTokens() {
1843
+ return __awaiter(this, void 0, void 0, function* () {
1844
+ let valid = false;
1845
+ try {
1846
+ const session = this.session.getCachedSession();
1847
+ if (!session) {
1848
+ throw null;
1849
+ }
1850
+ const result = yield firstValueFrom(this.checkAccessToken(session));
1851
+ if (!result.key) {
1852
+ throw null;
1853
+ }
1854
+ valid = true;
1855
+ }
1856
+ catch (error) {
1857
+ this.session.resetSessionCache();
1858
+ }
1859
+ return valid;
1860
+ });
1861
+ }
1805
1862
  checkAccessToken(session) {
1806
1863
  var _a;
1807
1864
  session = session || this.session.currentSession;
@@ -2623,7 +2680,8 @@ class EsolveCoupon {
2623
2680
  ;
2624
2681
 
2625
2682
  class EsolveCouponsService {
2626
- constructor(config, http, cookieService) {
2683
+ constructor(platformId, config, http, cookieService) {
2684
+ this.platformId = platformId;
2627
2685
  this.config = config;
2628
2686
  this.http = http;
2629
2687
  this.cookieService = cookieService;
@@ -2633,6 +2691,7 @@ class EsolveCouponsService {
2633
2691
  this.validation_error = new Subject();
2634
2692
  this.setStorageKey();
2635
2693
  this.coupons_cache = this.retrieveCache();
2694
+ this.is_browser = isPlatformBrowser(this.platformId);
2636
2695
  }
2637
2696
  onValidationError() {
2638
2697
  return this.validation_error.asObservable();
@@ -2712,11 +2771,10 @@ class EsolveCouponsService {
2712
2771
  this.deleteCoupons();
2713
2772
  }
2714
2773
  retrieveCache() {
2715
- if (this.cookieService.check(this.storage_key) ||
2716
- localStorage.getItem(this.storage_key)) {
2774
+ if (this.cookieService.check(this.storage_key) || this.getBackup()) {
2717
2775
  let coupons_json = this.cookieService.get(this.storage_key);
2718
- if (coupons_json === '' && localStorage) {
2719
- let local_store_data = localStorage.getItem(this.storage_key);
2776
+ if (coupons_json === '') {
2777
+ const local_store_data = this.getBackup();
2720
2778
  if (local_store_data) {
2721
2779
  coupons_json = local_store_data;
2722
2780
  }
@@ -2734,9 +2792,7 @@ class EsolveCouponsService {
2734
2792
  if (this.coupons_cache.size > 0) {
2735
2793
  const coupons_json = this.jsonEncodeMap(this.coupons_cache);
2736
2794
  this.cookieService.set(this.storage_key, coupons_json, undefined, '/');
2737
- if (localStorage) {
2738
- localStorage.setItem(this.storage_key, coupons_json);
2739
- }
2795
+ this.setBackup(coupons_json);
2740
2796
  }
2741
2797
  else {
2742
2798
  this.deleteCoupons();
@@ -2745,9 +2801,34 @@ class EsolveCouponsService {
2745
2801
  deleteCoupons() {
2746
2802
  this.coupons_cache.clear();
2747
2803
  this.cookieService.delete(this.storage_key, '/');
2748
- if (localStorage) {
2749
- localStorage.removeItem(this.storage_key);
2804
+ this.removeBackup();
2805
+ }
2806
+ getBackup() {
2807
+ if (!this.is_browser) {
2808
+ return null;
2809
+ }
2810
+ if (!localStorage) {
2811
+ return null;
2750
2812
  }
2813
+ return localStorage.getItem(this.storage_key);
2814
+ }
2815
+ setBackup(data) {
2816
+ if (!this.is_browser) {
2817
+ return;
2818
+ }
2819
+ if (!localStorage) {
2820
+ return;
2821
+ }
2822
+ localStorage.setItem(this.storage_key, data);
2823
+ }
2824
+ removeBackup() {
2825
+ if (!this.is_browser) {
2826
+ return;
2827
+ }
2828
+ if (!localStorage) {
2829
+ return;
2830
+ }
2831
+ localStorage.removeItem(this.storage_key);
2751
2832
  }
2752
2833
  addCoupon(id, key) {
2753
2834
  if (!this.coupons_cache.has(id)) {
@@ -2803,7 +2884,7 @@ class EsolveCouponsService {
2803
2884
  return coupons;
2804
2885
  }
2805
2886
  }
2806
- EsolveCouponsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveCouponsService, deps: [{ token: ESOLVE_CONNECT_CONFIG }, { token: i1$2.HttpClient }, { token: EsolveCookieService }], target: i0.ɵɵFactoryTarget.Injectable });
2887
+ EsolveCouponsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveCouponsService, deps: [{ token: PLATFORM_ID }, { token: ESOLVE_CONNECT_CONFIG }, { token: i1$2.HttpClient }, { token: EsolveCookieService }], target: i0.ɵɵFactoryTarget.Injectable });
2807
2888
  EsolveCouponsService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveCouponsService, providedIn: 'root' });
2808
2889
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveCouponsService, decorators: [{
2809
2890
  type: Injectable,
@@ -2812,6 +2893,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.3", ngImpor
2812
2893
  }]
2813
2894
  }], ctorParameters: function () {
2814
2895
  return [{ type: undefined, decorators: [{
2896
+ type: Inject,
2897
+ args: [PLATFORM_ID]
2898
+ }] }, { type: undefined, decorators: [{
2815
2899
  type: Inject,
2816
2900
  args: [ESOLVE_CONNECT_CONFIG]
2817
2901
  }] }, { type: i1$2.HttpClient }, { type: EsolveCookieService }];