tango-app-ui-shared 3.5.0-alpha.10 → 3.5.0-alpha.12

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.
@@ -73,7 +73,7 @@ class AuthService {
73
73
  return headers;
74
74
  }
75
75
  logout() {
76
- this.deleteCookie(this.authlocalStorageToken);
76
+ // this.deleteCookie(this.authlocalStorageToken);
77
77
  return this.http.get(`${this.userApiUrl}/logout`);
78
78
  }
79
79
  getClients() {
@@ -150,24 +150,6 @@ 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
- }
171
153
  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 });
172
154
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: AuthService, providedIn: 'root' });
173
155
  }
@@ -10931,131 +10913,28 @@ class HttpAuthInterceptor {
10931
10913
  }
10932
10914
  });
10933
10915
  }
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
- // }
11032
10916
  intercept(request, next) {
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 || '';
10917
+ const user = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
10918
+ request = request.clone({
10919
+ setHeaders: {
10920
+ Authorization: 'Bearer ' + user.authenticationToken
11039
10921
  }
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) => {
10922
+ });
10923
+ return next.handle(request)
10924
+ .pipe(tap((response) => {
11052
10925
  if (response?.body?.data?.result === 'RESTRICTED-IP') {
11053
10926
  this.router.navigateByUrl('/error/403-ip');
11054
10927
  }
11055
- }), catchError((error) => {
10928
+ }))
10929
+ .pipe(catchError((error) => {
11056
10930
  if (error instanceof HttpErrorResponse && error.status === 401) {
10931
+ // If the error is due to unauthorized access, try to refresh the token
11057
10932
  return this.handle401Error(request, next);
11058
10933
  }
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
+ // }
11059
10938
  return throwError(error);
11060
10939
  }));
11061
10940
  }
@@ -11063,44 +10942,70 @@ class HttpAuthInterceptor {
11063
10942
  if (!this.isRefreshingToken) {
11064
10943
  this.isRefreshingToken = true;
11065
10944
  return this.authService.refreshToken().pipe(switchMap((res) => {
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;
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
11069
10949
  request = request.clone({
11070
10950
  setHeaders: {
11071
- Authorization: 'Bearer ' + newToken,
11072
- },
10951
+ Authorization: `Bearer ${res.data.result.authenticationToken}`
10952
+ }
11073
10953
  });
10954
+ // Reset the flag for token refreshing
11074
10955
  this.isRefreshingToken = false;
10956
+ // Retry the original request with the new token
11075
10957
  return next.handle(request);
11076
10958
  }
11077
10959
  else {
11078
- return this.forceLogout('Token Expired. Please login again.');
11079
- }
11080
- }), catchError((err) => this.forceLogout(err)));
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
+ }));
11081
10995
  }
11082
10996
  else {
11083
- // Optional: implement logic to queue and retry if multiple requests during refresh
11084
- return throwError('Token refresh already in progress');
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
+ }));
11085
11007
  }
11086
11008
  }
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
- }
11104
11009
  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 });
11105
11010
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: HttpAuthInterceptor });
11106
11011
  }