tango-app-ui-shared 3.5.1-task.1 → 3.5.1-task.3

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.
@@ -150,6 +150,24 @@ class AuthService {
150
150
  getHeaderZone(data) {
151
151
  return this.http.post(`${this.trafficApiUrl}/headerZoneV2`, data);
152
152
  }
153
+ base64Encode(str) {
154
+ return btoa(encodeURIComponent(str));
155
+ }
156
+ base64Decode(str) {
157
+ return decodeURIComponent(atob(str));
158
+ }
159
+ setCookie(name, value, days = 1) {
160
+ const encodedValue = this.base64Encode(value);
161
+ const expires = new Date(Date.now() + days * 864e5).toUTCString();
162
+ document.cookie = `${name}=${encodedValue}; expires=${expires}; path=/; Secure; SameSite=Strict`;
163
+ }
164
+ getCookie(name) {
165
+ const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
166
+ return match ? this.base64Decode(match[2]) : null;
167
+ }
168
+ deleteCookie(name) {
169
+ document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
170
+ }
153
171
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: AuthService, deps: [{ token: i2.Router }, { token: i1.GlobalStateService }, { token: i3.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable });
154
172
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: AuthService, providedIn: 'root' });
155
173
  }
@@ -10913,28 +10931,131 @@ class HttpAuthInterceptor {
10913
10931
  }
10914
10932
  });
10915
10933
  }
10934
+ // intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
10935
+ // const user: any = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
10936
+ // request = request.clone({
10937
+ // setHeaders: {
10938
+ // Authorization: 'Bearer ' + user.authenticationToken
10939
+ // }
10940
+ // });
10941
+ // return next.handle(request)
10942
+ // .pipe(tap((response:any)=>{
10943
+ // if(response?.body?.data?.result === 'RESTRICTED-IP'){
10944
+ // this.router.navigateByUrl('/error/403-ip')
10945
+ // }
10946
+ // }))
10947
+ // .pipe(
10948
+ // catchError((error:any)=>{
10949
+ // if (error instanceof HttpErrorResponse && error.status === 401) {
10950
+ // // If the error is due to unauthorized access, try to refresh the token
10951
+ // return this.handle401Error(request, next);
10952
+ // }
10953
+ // // else if (error instanceof HttpErrorResponse && error.status === 403) {
10954
+ // // // If the error is due to unauthorized access, try to refresh the token
10955
+ // // this.router.navigate(['/manage/brands'])
10956
+ // // }
10957
+ // return throwError(error);
10958
+ // })
10959
+ // );
10960
+ // }
10961
+ // private handle401Error(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
10962
+ // if (!this.isRefreshingToken) {
10963
+ // this.isRefreshingToken = true;
10964
+ // return this.authService.refreshToken().pipe(
10965
+ // switchMap((res: any) => {
10966
+ // if (res && res.code == 200 && res.data.result) {
10967
+ // // Update local storage with the new token
10968
+ // localStorage.setItem(this.authlocalStorageToken, JSON.stringify(res.data.result));
10969
+ // // Clone the request with the new token
10970
+ // request = request.clone({
10971
+ // setHeaders: {
10972
+ // Authorization: `Bearer ${res.data.result.authenticationToken}`
10973
+ // }
10974
+ // });
10975
+ // // Reset the flag for token refreshing
10976
+ // this.isRefreshingToken = false;
10977
+ // // Retry the original request with the new token
10978
+ // return next.handle(request);
10979
+ // } else {
10980
+ // // Logout user if refresh token fails
10981
+ // this.authService.logout();
10982
+ // const keysToKeep = ['data-mismatch-draft'];
10983
+ // const valuesToKeep:any = {};
10984
+ // keysToKeep.forEach(key => {
10985
+ // const value = localStorage.getItem(key);
10986
+ // if (value !== null) {
10987
+ // valuesToKeep[key] = value;
10988
+ // }
10989
+ // });
10990
+ // localStorage.clear();
10991
+ // Object.keys(valuesToKeep).forEach(key => {
10992
+ // localStorage.setItem(key, valuesToKeep[key]);
10993
+ // });
10994
+ // this.router.navigate(['/auth/login']);
10995
+ // return throwError('Token Expired Please Login Again!');
10996
+ // }
10997
+ // }),
10998
+ // catchError((error) => {
10999
+ // // Logout user if refresh token fails
11000
+ // this.authService.logout();
11001
+ // const keysToKeep = ['data-mismatch-draft'];
11002
+ // const valuesToKeep:any = {};
11003
+ // keysToKeep.forEach(key => {
11004
+ // const value = localStorage.getItem(key);
11005
+ // if (value !== null) {
11006
+ // valuesToKeep[key] = value;
11007
+ // }
11008
+ // });
11009
+ // localStorage.clear();
11010
+ // Object.keys(valuesToKeep).forEach(key => {
11011
+ // localStorage.setItem(key, valuesToKeep[key]);
11012
+ // });
11013
+ // this.router.navigate(['/auth/login']);
11014
+ // return throwError(error);
11015
+ // })
11016
+ // );
11017
+ // } else {
11018
+ // // If already refreshing the token, queue the request and wait
11019
+ // return this.tokenRefreshed.pipe(
11020
+ // switchMap(() => {
11021
+ // const tokens: any = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
11022
+ // request = request.clone({
11023
+ // setHeaders: {
11024
+ // Authorization: 'Bearer ' + tokens.authenticationToken
11025
+ // }
11026
+ // });
11027
+ // return next.handle(request);
11028
+ // })
11029
+ // );
11030
+ // }
11031
+ // }
10916
11032
  intercept(request, next) {
10917
- const user = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
10918
- request = request.clone({
10919
- setHeaders: {
10920
- Authorization: 'Bearer ' + user.authenticationToken
11033
+ const tokenObjStr = this.authService.getCookie(this.authlocalStorageToken);
11034
+ let token = '';
11035
+ if (tokenObjStr) {
11036
+ try {
11037
+ const tokenObj = JSON.parse(tokenObjStr);
11038
+ token = tokenObj?.authenticationToken || '';
10921
11039
  }
10922
- });
10923
- return next.handle(request)
10924
- .pipe(tap((response) => {
11040
+ catch (e) {
11041
+ console.error('Invalid auth token format in cookie', e);
11042
+ }
11043
+ }
11044
+ if (token) {
11045
+ request = request.clone({
11046
+ setHeaders: {
11047
+ Authorization: 'Bearer ' + token,
11048
+ },
11049
+ });
11050
+ }
11051
+ return next.handle(request).pipe(tap((response) => {
10925
11052
  if (response?.body?.data?.result === 'RESTRICTED-IP') {
10926
11053
  this.router.navigateByUrl('/error/403-ip');
10927
11054
  }
10928
- }))
10929
- .pipe(catchError((error) => {
11055
+ }), catchError((error) => {
10930
11056
  if (error instanceof HttpErrorResponse && error.status === 401) {
10931
- // If the error is due to unauthorized access, try to refresh the token
10932
11057
  return this.handle401Error(request, next);
10933
11058
  }
10934
- // else if (error instanceof HttpErrorResponse && error.status === 403) {
10935
- // // If the error is due to unauthorized access, try to refresh the token
10936
- // this.router.navigate(['/manage/brands'])
10937
- // }
10938
11059
  return throwError(error);
10939
11060
  }));
10940
11061
  }
@@ -10942,70 +11063,44 @@ class HttpAuthInterceptor {
10942
11063
  if (!this.isRefreshingToken) {
10943
11064
  this.isRefreshingToken = true;
10944
11065
  return this.authService.refreshToken().pipe(switchMap((res) => {
10945
- if (res && res.code == 200 && res.data.result) {
10946
- // Update local storage with the new token
10947
- localStorage.setItem(this.authlocalStorageToken, JSON.stringify(res.data.result));
10948
- // Clone the request with the new token
11066
+ if (res && res.code === 200 && res.data?.result) {
11067
+ this.authService.setCookie(this.authlocalStorageToken, JSON.stringify(res.data.result), 1);
11068
+ const newToken = res.data.result.authenticationToken;
10949
11069
  request = request.clone({
10950
11070
  setHeaders: {
10951
- Authorization: `Bearer ${res.data.result.authenticationToken}`
10952
- }
11071
+ Authorization: 'Bearer ' + newToken,
11072
+ },
10953
11073
  });
10954
- // Reset the flag for token refreshing
10955
11074
  this.isRefreshingToken = false;
10956
- // Retry the original request with the new token
10957
11075
  return next.handle(request);
10958
11076
  }
10959
11077
  else {
10960
- // Logout user if refresh token fails
10961
- this.authService.logout();
10962
- const keysToKeep = ['data-mismatch-draft'];
10963
- const valuesToKeep = {};
10964
- keysToKeep.forEach(key => {
10965
- const value = localStorage.getItem(key);
10966
- if (value !== null) {
10967
- valuesToKeep[key] = value;
10968
- }
10969
- });
10970
- localStorage.clear();
10971
- Object.keys(valuesToKeep).forEach(key => {
10972
- localStorage.setItem(key, valuesToKeep[key]);
10973
- });
10974
- this.router.navigate(['/auth/login']);
10975
- return throwError('Token Expired Please Login Again!');
10976
- }
10977
- }), catchError((error) => {
10978
- // Logout user if refresh token fails
10979
- this.authService.logout();
10980
- const keysToKeep = ['data-mismatch-draft'];
10981
- const valuesToKeep = {};
10982
- keysToKeep.forEach(key => {
10983
- const value = localStorage.getItem(key);
10984
- if (value !== null) {
10985
- valuesToKeep[key] = value;
10986
- }
10987
- });
10988
- localStorage.clear();
10989
- Object.keys(valuesToKeep).forEach(key => {
10990
- localStorage.setItem(key, valuesToKeep[key]);
10991
- });
10992
- this.router.navigate(['/auth/login']);
10993
- return throwError(error);
10994
- }));
11078
+ return this.forceLogout('Token Expired. Please login again.');
11079
+ }
11080
+ }), catchError((err) => this.forceLogout(err)));
10995
11081
  }
10996
11082
  else {
10997
- // If already refreshing the token, queue the request and wait
10998
- return this.tokenRefreshed.pipe(switchMap(() => {
10999
- const tokens = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
11000
- request = request.clone({
11001
- setHeaders: {
11002
- Authorization: 'Bearer ' + tokens.authenticationToken
11003
- }
11004
- });
11005
- return next.handle(request);
11006
- }));
11083
+ // Optional: implement logic to queue and retry if multiple requests during refresh
11084
+ return throwError('Token refresh already in progress');
11007
11085
  }
11008
11086
  }
11087
+ forceLogout(message) {
11088
+ this.authService.logout();
11089
+ const keysToKeep = ['data-mismatch-draft'];
11090
+ const valuesToKeep = {};
11091
+ keysToKeep.forEach((key) => {
11092
+ const val = localStorage.getItem(key);
11093
+ if (val)
11094
+ valuesToKeep[key] = val;
11095
+ });
11096
+ localStorage.clear();
11097
+ // Object.entries(valuesToKeep).forEach(([k, v]) => {
11098
+ // return localStorage.setItem(k, v);
11099
+ // });
11100
+ this.authService.deleteCookie(this.authlocalStorageToken);
11101
+ this.router.navigate(['/auth/login']);
11102
+ return throwError(() => message);
11103
+ }
11009
11104
  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 });
11010
11105
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: HttpAuthInterceptor });
11011
11106
  }