tango-app-ui-shared 3.5.0-alpha.3 → 3.5.0-alpha.4

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,9 +73,8 @@ class AuthService {
73
73
  return headers;
74
74
  }
75
75
  logout() {
76
- return this.http.get(`${this.userApiUrl}/logout`, {
77
- headers: this.getHeaders()
78
- });
76
+ this.deleteCookie(this.authlocalStorageToken);
77
+ return this.http.get(`${this.userApiUrl}/logout`);
79
78
  }
80
79
  getClients() {
81
80
  return this.http.get(`${this.clientApiUrl}/get-clients`, {})
@@ -151,6 +150,24 @@ class AuthService {
151
150
  getHeaderZone(data) {
152
151
  return this.http.post(`${this.trafficApiUrl}/headerZoneV2`, data);
153
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
+ }
154
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 });
155
172
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: AuthService, providedIn: 'root' });
156
173
  }
@@ -8667,9 +8684,7 @@ class SidebarFooterComponent {
8667
8684
  }
8668
8685
  logout() {
8669
8686
  this.auth.logout();
8670
- // .pipe(takeUntil(this.destroy$)).subscribe((res:any)=>{
8671
8687
  this.router.navigate(["/auth/login"]);
8672
- // })
8673
8688
  localStorage.clear();
8674
8689
  this.pageInfo.setTitle('login');
8675
8690
  }
@@ -10905,28 +10920,131 @@ class HttpAuthInterceptor {
10905
10920
  }
10906
10921
  });
10907
10922
  }
10923
+ // intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
10924
+ // const user: any = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
10925
+ // request = request.clone({
10926
+ // setHeaders: {
10927
+ // Authorization: 'Bearer ' + user.authenticationToken
10928
+ // }
10929
+ // });
10930
+ // return next.handle(request)
10931
+ // .pipe(tap((response:any)=>{
10932
+ // if(response?.body?.data?.result === 'RESTRICTED-IP'){
10933
+ // this.router.navigateByUrl('/error/403-ip')
10934
+ // }
10935
+ // }))
10936
+ // .pipe(
10937
+ // catchError((error:any)=>{
10938
+ // if (error instanceof HttpErrorResponse && error.status === 401) {
10939
+ // // If the error is due to unauthorized access, try to refresh the token
10940
+ // return this.handle401Error(request, next);
10941
+ // }
10942
+ // // else if (error instanceof HttpErrorResponse && error.status === 403) {
10943
+ // // // If the error is due to unauthorized access, try to refresh the token
10944
+ // // this.router.navigate(['/manage/brands'])
10945
+ // // }
10946
+ // return throwError(error);
10947
+ // })
10948
+ // );
10949
+ // }
10950
+ // private handle401Error(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
10951
+ // if (!this.isRefreshingToken) {
10952
+ // this.isRefreshingToken = true;
10953
+ // return this.authService.refreshToken().pipe(
10954
+ // switchMap((res: any) => {
10955
+ // if (res && res.code == 200 && res.data.result) {
10956
+ // // Update local storage with the new token
10957
+ // localStorage.setItem(this.authlocalStorageToken, JSON.stringify(res.data.result));
10958
+ // // Clone the request with the new token
10959
+ // request = request.clone({
10960
+ // setHeaders: {
10961
+ // Authorization: `Bearer ${res.data.result.authenticationToken}`
10962
+ // }
10963
+ // });
10964
+ // // Reset the flag for token refreshing
10965
+ // this.isRefreshingToken = false;
10966
+ // // Retry the original request with the new token
10967
+ // return next.handle(request);
10968
+ // } else {
10969
+ // // Logout user if refresh token fails
10970
+ // this.authService.logout();
10971
+ // const keysToKeep = ['data-mismatch-draft'];
10972
+ // const valuesToKeep:any = {};
10973
+ // keysToKeep.forEach(key => {
10974
+ // const value = localStorage.getItem(key);
10975
+ // if (value !== null) {
10976
+ // valuesToKeep[key] = value;
10977
+ // }
10978
+ // });
10979
+ // localStorage.clear();
10980
+ // Object.keys(valuesToKeep).forEach(key => {
10981
+ // localStorage.setItem(key, valuesToKeep[key]);
10982
+ // });
10983
+ // this.router.navigate(['/auth/login']);
10984
+ // return throwError('Token Expired Please Login Again!');
10985
+ // }
10986
+ // }),
10987
+ // catchError((error) => {
10988
+ // // Logout user if refresh token fails
10989
+ // this.authService.logout();
10990
+ // const keysToKeep = ['data-mismatch-draft'];
10991
+ // const valuesToKeep:any = {};
10992
+ // keysToKeep.forEach(key => {
10993
+ // const value = localStorage.getItem(key);
10994
+ // if (value !== null) {
10995
+ // valuesToKeep[key] = value;
10996
+ // }
10997
+ // });
10998
+ // localStorage.clear();
10999
+ // Object.keys(valuesToKeep).forEach(key => {
11000
+ // localStorage.setItem(key, valuesToKeep[key]);
11001
+ // });
11002
+ // this.router.navigate(['/auth/login']);
11003
+ // return throwError(error);
11004
+ // })
11005
+ // );
11006
+ // } else {
11007
+ // // If already refreshing the token, queue the request and wait
11008
+ // return this.tokenRefreshed.pipe(
11009
+ // switchMap(() => {
11010
+ // const tokens: any = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
11011
+ // request = request.clone({
11012
+ // setHeaders: {
11013
+ // Authorization: 'Bearer ' + tokens.authenticationToken
11014
+ // }
11015
+ // });
11016
+ // return next.handle(request);
11017
+ // })
11018
+ // );
11019
+ // }
11020
+ // }
10908
11021
  intercept(request, next) {
10909
- const user = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
10910
- request = request.clone({
10911
- setHeaders: {
10912
- Authorization: 'Bearer ' + user.authenticationToken
11022
+ const tokenObjStr = this.authService.getCookie(this.authlocalStorageToken);
11023
+ let token = '';
11024
+ if (tokenObjStr) {
11025
+ try {
11026
+ const tokenObj = JSON.parse(tokenObjStr);
11027
+ token = tokenObj?.authenticationToken || '';
10913
11028
  }
10914
- });
10915
- return next.handle(request)
10916
- .pipe(tap((response) => {
11029
+ catch (e) {
11030
+ console.error('Invalid auth token format in cookie', e);
11031
+ }
11032
+ }
11033
+ if (token) {
11034
+ request = request.clone({
11035
+ setHeaders: {
11036
+ Authorization: 'Bearer ' + token,
11037
+ },
11038
+ });
11039
+ }
11040
+ return next.handle(request).pipe(tap((response) => {
10917
11041
  if (response?.body?.data?.result === 'RESTRICTED-IP') {
10918
11042
  this.router.navigateByUrl('/error/403-ip');
10919
11043
  }
10920
- }))
10921
- .pipe(catchError((error) => {
11044
+ }), catchError((error) => {
10922
11045
  if (error instanceof HttpErrorResponse && error.status === 401) {
10923
- // If the error is due to unauthorized access, try to refresh the token
10924
11046
  return this.handle401Error(request, next);
10925
11047
  }
10926
- // else if (error instanceof HttpErrorResponse && error.status === 403) {
10927
- // // If the error is due to unauthorized access, try to refresh the token
10928
- // this.router.navigate(['/manage/brands'])
10929
- // }
10930
11048
  return throwError(error);
10931
11049
  }));
10932
11050
  }
@@ -10934,70 +11052,44 @@ class HttpAuthInterceptor {
10934
11052
  if (!this.isRefreshingToken) {
10935
11053
  this.isRefreshingToken = true;
10936
11054
  return this.authService.refreshToken().pipe(switchMap((res) => {
10937
- if (res && res.code == 200 && res.data.result) {
10938
- // Update local storage with the new token
10939
- localStorage.setItem(this.authlocalStorageToken, JSON.stringify(res.data.result));
10940
- // Clone the request with the new token
11055
+ if (res && res.code === 200 && res.data?.result) {
11056
+ this.authService.setCookie(this.authlocalStorageToken, JSON.stringify(res.data.result), 7);
11057
+ const newToken = res.data.result.authenticationToken;
10941
11058
  request = request.clone({
10942
11059
  setHeaders: {
10943
- Authorization: `Bearer ${res.data.result.authenticationToken}`
10944
- }
11060
+ Authorization: 'Bearer ' + newToken,
11061
+ },
10945
11062
  });
10946
- // Reset the flag for token refreshing
10947
11063
  this.isRefreshingToken = false;
10948
- // Retry the original request with the new token
10949
11064
  return next.handle(request);
10950
11065
  }
10951
11066
  else {
10952
- // Logout user if refresh token fails
10953
- this.authService.logout();
10954
- const keysToKeep = ['data-mismatch-draft'];
10955
- const valuesToKeep = {};
10956
- keysToKeep.forEach(key => {
10957
- const value = localStorage.getItem(key);
10958
- if (value !== null) {
10959
- valuesToKeep[key] = value;
10960
- }
10961
- });
10962
- localStorage.clear();
10963
- Object.keys(valuesToKeep).forEach(key => {
10964
- localStorage.setItem(key, valuesToKeep[key]);
10965
- });
10966
- this.router.navigate(['/auth/login']);
10967
- return throwError('Token Expired Please Login Again!');
10968
- }
10969
- }), catchError((error) => {
10970
- // Logout user if refresh token fails
10971
- this.authService.logout();
10972
- const keysToKeep = ['data-mismatch-draft'];
10973
- const valuesToKeep = {};
10974
- keysToKeep.forEach(key => {
10975
- const value = localStorage.getItem(key);
10976
- if (value !== null) {
10977
- valuesToKeep[key] = value;
10978
- }
10979
- });
10980
- localStorage.clear();
10981
- Object.keys(valuesToKeep).forEach(key => {
10982
- localStorage.setItem(key, valuesToKeep[key]);
10983
- });
10984
- this.router.navigate(['/auth/login']);
10985
- return throwError(error);
10986
- }));
11067
+ return this.forceLogout('Token Expired. Please login again.');
11068
+ }
11069
+ }), catchError((err) => this.forceLogout(err)));
10987
11070
  }
10988
11071
  else {
10989
- // If already refreshing the token, queue the request and wait
10990
- return this.tokenRefreshed.pipe(switchMap(() => {
10991
- const tokens = JSON.parse(localStorage.getItem(this.authlocalStorageToken) || '{}');
10992
- request = request.clone({
10993
- setHeaders: {
10994
- Authorization: 'Bearer ' + tokens.authenticationToken
10995
- }
10996
- });
10997
- return next.handle(request);
10998
- }));
11072
+ // Optional: implement logic to queue and retry if multiple requests during refresh
11073
+ return throwError('Token refresh already in progress');
10999
11074
  }
11000
11075
  }
11076
+ forceLogout(message) {
11077
+ this.authService.logout();
11078
+ const keysToKeep = ['data-mismatch-draft'];
11079
+ const valuesToKeep = {};
11080
+ keysToKeep.forEach((key) => {
11081
+ const val = localStorage.getItem(key);
11082
+ if (val)
11083
+ valuesToKeep[key] = val;
11084
+ });
11085
+ localStorage.clear();
11086
+ // Object.entries(valuesToKeep).forEach(([k, v]) => {
11087
+ // return localStorage.setItem(k, v);
11088
+ // });
11089
+ this.authService.deleteCookie(this.authlocalStorageToken);
11090
+ this.router.navigate(['/auth/login']);
11091
+ return throwError(() => message);
11092
+ }
11001
11093
  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 });
11002
11094
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: HttpAuthInterceptor });
11003
11095
  }