@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.
@@ -164,6 +164,26 @@ class EsolveSessionService {
164
164
  clearTimeout(this.key_expiration_timer);
165
165
  }
166
166
  }
167
+ getCachedSession() {
168
+ let json_data = this.cookieService.get(this.storage_key);
169
+ if (json_data === '') {
170
+ const local_store_data = this.getBackup();
171
+ if (local_store_data) {
172
+ json_data = local_store_data;
173
+ }
174
+ }
175
+ const stored_session = (json_data ? JSON.parse(json_data) : null);
176
+ if (!stored_session) {
177
+ return null;
178
+ }
179
+ const expiration_date = new Date(stored_session.expiration_date);
180
+ const loaded_session = new EsolveSession(stored_session.id, stored_session.location_id, stored_session.key, expiration_date);
181
+ return loaded_session;
182
+ }
183
+ resetSessionCache() {
184
+ this.cookieService.delete(this.storage_key, '/');
185
+ this.removeBackup();
186
+ }
167
187
  restore(expirationCallback, invalidSessionCallback) {
168
188
  if (this.currentSession.valid) {
169
189
  return;
@@ -245,6 +265,15 @@ class EsolveSessionService {
245
265
  }
246
266
  localStorage.setItem(this.storage_key, data);
247
267
  }
268
+ removeBackup() {
269
+ if (!this.is_browser) {
270
+ return;
271
+ }
272
+ if (!localStorage) {
273
+ return;
274
+ }
275
+ localStorage.removeItem(this.storage_key);
276
+ }
248
277
  }
249
278
  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 });
250
279
  EsolveSessionService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveSessionService, providedIn: 'root' });
@@ -1685,6 +1714,11 @@ class EsolveAuthService {
1685
1714
  this.errorHandler = errorHandler;
1686
1715
  this.cookieService = cookieService;
1687
1716
  }
1717
+ static preValidate(auth) {
1718
+ return () => {
1719
+ return auth.validateCachedTokens();
1720
+ };
1721
+ }
1688
1722
  getAccessToken(email, password, anonymous = false) {
1689
1723
  let params = new HttpParams();
1690
1724
  if (anonymous) {
@@ -1712,7 +1746,8 @@ class EsolveAuthService {
1712
1746
  this.handleAuthentication(responseData.additional_data);
1713
1747
  }));
1714
1748
  }
1715
- autoLogin() {
1749
+ async autoLogin() {
1750
+ await this.validateCachedTokens();
1716
1751
  this.session.restore(this.handleExpiration(), () => {
1717
1752
  firstValueFrom(this.getAccessToken('', '', true)).then(() => {
1718
1753
  // empty
@@ -1773,6 +1808,24 @@ class EsolveAuthService {
1773
1808
  }
1774
1809
  throw new Error('Invalid response');
1775
1810
  }
1811
+ async validateCachedTokens() {
1812
+ let valid = false;
1813
+ try {
1814
+ const session = this.session.getCachedSession();
1815
+ if (!session) {
1816
+ throw null;
1817
+ }
1818
+ const result = await firstValueFrom(this.checkAccessToken(session));
1819
+ if (!result.key) {
1820
+ throw null;
1821
+ }
1822
+ valid = true;
1823
+ }
1824
+ catch (error) {
1825
+ this.session.resetSessionCache();
1826
+ }
1827
+ return valid;
1828
+ }
1776
1829
  checkAccessToken(session) {
1777
1830
  session = session || this.session.currentSession;
1778
1831
  const token = session?.key ?? '';
@@ -2579,7 +2632,8 @@ class EsolveCoupon {
2579
2632
  ;
2580
2633
 
2581
2634
  class EsolveCouponsService {
2582
- constructor(config, http, cookieService) {
2635
+ constructor(platformId, config, http, cookieService) {
2636
+ this.platformId = platformId;
2583
2637
  this.config = config;
2584
2638
  this.http = http;
2585
2639
  this.cookieService = cookieService;
@@ -2589,6 +2643,7 @@ class EsolveCouponsService {
2589
2643
  this.validation_error = new Subject();
2590
2644
  this.setStorageKey();
2591
2645
  this.coupons_cache = this.retrieveCache();
2646
+ this.is_browser = isPlatformBrowser(this.platformId);
2592
2647
  }
2593
2648
  onValidationError() {
2594
2649
  return this.validation_error.asObservable();
@@ -2666,11 +2721,10 @@ class EsolveCouponsService {
2666
2721
  this.deleteCoupons();
2667
2722
  }
2668
2723
  retrieveCache() {
2669
- if (this.cookieService.check(this.storage_key) ||
2670
- localStorage.getItem(this.storage_key)) {
2724
+ if (this.cookieService.check(this.storage_key) || this.getBackup()) {
2671
2725
  let coupons_json = this.cookieService.get(this.storage_key);
2672
- if (coupons_json === '' && localStorage) {
2673
- let local_store_data = localStorage.getItem(this.storage_key);
2726
+ if (coupons_json === '') {
2727
+ const local_store_data = this.getBackup();
2674
2728
  if (local_store_data) {
2675
2729
  coupons_json = local_store_data;
2676
2730
  }
@@ -2688,9 +2742,7 @@ class EsolveCouponsService {
2688
2742
  if (this.coupons_cache.size > 0) {
2689
2743
  const coupons_json = this.jsonEncodeMap(this.coupons_cache);
2690
2744
  this.cookieService.set(this.storage_key, coupons_json, undefined, '/');
2691
- if (localStorage) {
2692
- localStorage.setItem(this.storage_key, coupons_json);
2693
- }
2745
+ this.setBackup(coupons_json);
2694
2746
  }
2695
2747
  else {
2696
2748
  this.deleteCoupons();
@@ -2699,9 +2751,34 @@ class EsolveCouponsService {
2699
2751
  deleteCoupons() {
2700
2752
  this.coupons_cache.clear();
2701
2753
  this.cookieService.delete(this.storage_key, '/');
2702
- if (localStorage) {
2703
- localStorage.removeItem(this.storage_key);
2754
+ this.removeBackup();
2755
+ }
2756
+ getBackup() {
2757
+ if (!this.is_browser) {
2758
+ return null;
2704
2759
  }
2760
+ if (!localStorage) {
2761
+ return null;
2762
+ }
2763
+ return localStorage.getItem(this.storage_key);
2764
+ }
2765
+ setBackup(data) {
2766
+ if (!this.is_browser) {
2767
+ return;
2768
+ }
2769
+ if (!localStorage) {
2770
+ return;
2771
+ }
2772
+ localStorage.setItem(this.storage_key, data);
2773
+ }
2774
+ removeBackup() {
2775
+ if (!this.is_browser) {
2776
+ return;
2777
+ }
2778
+ if (!localStorage) {
2779
+ return;
2780
+ }
2781
+ localStorage.removeItem(this.storage_key);
2705
2782
  }
2706
2783
  addCoupon(id, key) {
2707
2784
  if (!this.coupons_cache.has(id)) {
@@ -2757,7 +2834,7 @@ class EsolveCouponsService {
2757
2834
  return coupons;
2758
2835
  }
2759
2836
  }
2760
- 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 });
2837
+ 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 });
2761
2838
  EsolveCouponsService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveCouponsService, providedIn: 'root' });
2762
2839
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.3", ngImport: i0, type: EsolveCouponsService, decorators: [{
2763
2840
  type: Injectable,
@@ -2765,6 +2842,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.3", ngImpor
2765
2842
  providedIn: 'root',
2766
2843
  }]
2767
2844
  }], ctorParameters: function () { return [{ type: undefined, decorators: [{
2845
+ type: Inject,
2846
+ args: [PLATFORM_ID]
2847
+ }] }, { type: undefined, decorators: [{
2768
2848
  type: Inject,
2769
2849
  args: [ESOLVE_CONNECT_CONFIG]
2770
2850
  }] }, { type: i1$2.HttpClient }, { type: EsolveCookieService }]; } });