tango-app-ui-shared 3.6.0-cookie-4 → 3.6.0-cookie-2

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.
@@ -76,7 +76,7 @@ class AuthService {
76
76
  return headers;
77
77
  }
78
78
  logout() {
79
- // this.deleteCookie(this.authlocalStorageToken);
79
+ this.deleteCookie(this.authlocalStorageToken);
80
80
  return this.http.get(`${this.userApiUrl}/logout`);
81
81
  }
82
82
  getClients() {
@@ -11191,28 +11191,131 @@ class HttpAuthInterceptor {
11191
11191
  }
11192
11192
  });
11193
11193
  }
11194
+ // intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
11195
+ // const user: any = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
11196
+ // request = request.clone({
11197
+ // setHeaders: {
11198
+ // Authorization: 'Bearer ' + user.authenticationToken
11199
+ // }
11200
+ // });
11201
+ // return next.handle(request)
11202
+ // .pipe(tap((response:any)=>{
11203
+ // if(response?.body?.data?.result === 'RESTRICTED-IP'){
11204
+ // this.router.navigateByUrl('/error/403-ip')
11205
+ // }
11206
+ // }))
11207
+ // .pipe(
11208
+ // catchError((error:any)=>{
11209
+ // if (error instanceof HttpErrorResponse && error.status === 401) {
11210
+ // // If the error is due to unauthorized access, try to refresh the token
11211
+ // return this.handle401Error(request, next);
11212
+ // }
11213
+ // // else if (error instanceof HttpErrorResponse && error.status === 403) {
11214
+ // // // If the error is due to unauthorized access, try to refresh the token
11215
+ // // this.router.navigate(['/manage/brands'])
11216
+ // // }
11217
+ // return throwError(error);
11218
+ // })
11219
+ // );
11220
+ // }
11221
+ // private handle401Error(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
11222
+ // if (!this.isRefreshingToken) {
11223
+ // this.isRefreshingToken = true;
11224
+ // return this.authService.refreshToken().pipe(
11225
+ // switchMap((res: any) => {
11226
+ // if (res && res.code == 200 && res.data.result) {
11227
+ // // Update local storage with the new token
11228
+ // localStorage.setItem(this.authlocalStorageToken, JSON.stringify(res.data.result));
11229
+ // // Clone the request with the new token
11230
+ // request = request.clone({
11231
+ // setHeaders: {
11232
+ // Authorization: `Bearer ${res.data.result.authenticationToken}`
11233
+ // }
11234
+ // });
11235
+ // // Reset the flag for token refreshing
11236
+ // this.isRefreshingToken = false;
11237
+ // // Retry the original request with the new token
11238
+ // return next.handle(request);
11239
+ // } else {
11240
+ // // Logout user if refresh token fails
11241
+ // this.authService.logout();
11242
+ // const keysToKeep = ['data-mismatch-draft'];
11243
+ // const valuesToKeep:any = {};
11244
+ // keysToKeep.forEach(key => {
11245
+ // const value = localStorage.getItem(key);
11246
+ // if (value !== null) {
11247
+ // valuesToKeep[key] = value;
11248
+ // }
11249
+ // });
11250
+ // localStorage.clear();
11251
+ // Object.keys(valuesToKeep).forEach(key => {
11252
+ // localStorage.setItem(key, valuesToKeep[key]);
11253
+ // });
11254
+ // this.router.navigate(['/auth/login']);
11255
+ // return throwError('Token Expired Please Login Again!');
11256
+ // }
11257
+ // }),
11258
+ // catchError((error) => {
11259
+ // // Logout user if refresh token fails
11260
+ // this.authService.logout();
11261
+ // const keysToKeep = ['data-mismatch-draft'];
11262
+ // const valuesToKeep:any = {};
11263
+ // keysToKeep.forEach(key => {
11264
+ // const value = localStorage.getItem(key);
11265
+ // if (value !== null) {
11266
+ // valuesToKeep[key] = value;
11267
+ // }
11268
+ // });
11269
+ // localStorage.clear();
11270
+ // Object.keys(valuesToKeep).forEach(key => {
11271
+ // localStorage.setItem(key, valuesToKeep[key]);
11272
+ // });
11273
+ // this.router.navigate(['/auth/login']);
11274
+ // return throwError(error);
11275
+ // })
11276
+ // );
11277
+ // } else {
11278
+ // // If already refreshing the token, queue the request and wait
11279
+ // return this.tokenRefreshed.pipe(
11280
+ // switchMap(() => {
11281
+ // const tokens: any = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
11282
+ // request = request.clone({
11283
+ // setHeaders: {
11284
+ // Authorization: 'Bearer ' + tokens.authenticationToken
11285
+ // }
11286
+ // });
11287
+ // return next.handle(request);
11288
+ // })
11289
+ // );
11290
+ // }
11291
+ // }
11194
11292
  intercept(request, next) {
11195
- const user = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
11196
- request = request.clone({
11197
- setHeaders: {
11198
- Authorization: 'Bearer ' + user.authenticationToken
11293
+ const tokenObjStr = this.authService.getCookie(this.authlocalStorageToken);
11294
+ let token = '';
11295
+ if (tokenObjStr) {
11296
+ try {
11297
+ const tokenObj = JSON.parse(tokenObjStr);
11298
+ token = tokenObj?.authenticationToken || '';
11199
11299
  }
11200
- });
11201
- return next.handle(request)
11202
- .pipe(tap((response) => {
11300
+ catch (e) {
11301
+ console.error('Invalid auth token format in cookie', e);
11302
+ }
11303
+ }
11304
+ if (token) {
11305
+ request = request.clone({
11306
+ setHeaders: {
11307
+ Authorization: 'Bearer ' + token,
11308
+ },
11309
+ });
11310
+ }
11311
+ return next.handle(request).pipe(tap((response) => {
11203
11312
  if (response?.body?.data?.result === 'RESTRICTED-IP') {
11204
11313
  this.router.navigateByUrl('/error/403-ip');
11205
11314
  }
11206
- }))
11207
- .pipe(catchError((error) => {
11315
+ }), catchError((error) => {
11208
11316
  if (error instanceof HttpErrorResponse && error.status === 401) {
11209
- // If the error is due to unauthorized access, try to refresh the token
11210
11317
  return this.handle401Error(request, next);
11211
11318
  }
11212
- // else if (error instanceof HttpErrorResponse && error.status === 403) {
11213
- // // If the error is due to unauthorized access, try to refresh the token
11214
- // this.router.navigate(['/manage/brands'])
11215
- // }
11216
11319
  return throwError(error);
11217
11320
  }));
11218
11321
  }
@@ -11220,70 +11323,44 @@ class HttpAuthInterceptor {
11220
11323
  if (!this.isRefreshingToken) {
11221
11324
  this.isRefreshingToken = true;
11222
11325
  return this.authService.refreshToken().pipe(switchMap((res) => {
11223
- if (res && res.code == 200 && res.data.result) {
11224
- // Update local storage with the new token
11225
- localStorage.setItem(this.authlocalStorageToken, JSON.stringify(res.data.result));
11226
- // Clone the request with the new token
11326
+ if (res && res.code === 200 && res.data?.result) {
11327
+ this.authService.setCookie(this.authlocalStorageToken, JSON.stringify(res.data.result), 1);
11328
+ const newToken = res.data.result.authenticationToken;
11227
11329
  request = request.clone({
11228
11330
  setHeaders: {
11229
- Authorization: `Bearer ${res.data.result.authenticationToken}`
11230
- }
11331
+ Authorization: 'Bearer ' + newToken,
11332
+ },
11231
11333
  });
11232
- // Reset the flag for token refreshing
11233
11334
  this.isRefreshingToken = false;
11234
- // Retry the original request with the new token
11235
11335
  return next.handle(request);
11236
11336
  }
11237
11337
  else {
11238
- // Logout user if refresh token fails
11239
- this.authService.logout();
11240
- const keysToKeep = ['data-mismatch-draft'];
11241
- const valuesToKeep = {};
11242
- keysToKeep.forEach(key => {
11243
- const value = localStorage.getItem(key);
11244
- if (value !== null) {
11245
- valuesToKeep[key] = value;
11246
- }
11247
- });
11248
- localStorage.clear();
11249
- Object.keys(valuesToKeep).forEach(key => {
11250
- localStorage.setItem(key, valuesToKeep[key]);
11251
- });
11252
- this.router.navigate(['/auth/login']);
11253
- return throwError('Token Expired Please Login Again!');
11254
- }
11255
- }), catchError((error) => {
11256
- // Logout user if refresh token fails
11257
- this.authService.logout();
11258
- const keysToKeep = ['data-mismatch-draft'];
11259
- const valuesToKeep = {};
11260
- keysToKeep.forEach(key => {
11261
- const value = localStorage.getItem(key);
11262
- if (value !== null) {
11263
- valuesToKeep[key] = value;
11264
- }
11265
- });
11266
- localStorage.clear();
11267
- Object.keys(valuesToKeep).forEach(key => {
11268
- localStorage.setItem(key, valuesToKeep[key]);
11269
- });
11270
- this.router.navigate(['/auth/login']);
11271
- return throwError(error);
11272
- }));
11338
+ return this.forceLogout('Token Expired. Please login again.');
11339
+ }
11340
+ }), catchError((err) => this.forceLogout(err)));
11273
11341
  }
11274
11342
  else {
11275
- // If already refreshing the token, queue the request and wait
11276
- return this.tokenRefreshed.pipe(switchMap(() => {
11277
- const tokens = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
11278
- request = request.clone({
11279
- setHeaders: {
11280
- Authorization: 'Bearer ' + tokens.authenticationToken
11281
- }
11282
- });
11283
- return next.handle(request);
11284
- }));
11343
+ // Optional: implement logic to queue and retry if multiple requests during refresh
11344
+ return throwError('Token refresh already in progress');
11285
11345
  }
11286
11346
  }
11347
+ forceLogout(message) {
11348
+ this.authService.logout();
11349
+ const keysToKeep = ['data-mismatch-draft'];
11350
+ const valuesToKeep = {};
11351
+ keysToKeep.forEach((key) => {
11352
+ const val = localStorage.getItem(key);
11353
+ if (val)
11354
+ valuesToKeep[key] = val;
11355
+ });
11356
+ localStorage.clear();
11357
+ // Object.entries(valuesToKeep).forEach(([k, v]) => {
11358
+ // return localStorage.setItem(k, v);
11359
+ // });
11360
+ this.authService.deleteCookie(this.authlocalStorageToken);
11361
+ this.router.navigate(['/auth/login']);
11362
+ return throwError(() => message);
11363
+ }
11287
11364
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: HttpAuthInterceptor, deps: [{ token: i1.GlobalStateService }, { token: AuthService }, { token: i2.Router }], target: i0.ɵɵFactoryTarget.Injectable });
11288
11365
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: HttpAuthInterceptor });
11289
11366
  }