shared-ritm 1.3.43 → 1.3.45

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.
@@ -4,11 +4,13 @@ export declare enum ApiServiceType {
4
4
  }
5
5
  export default class ApiService {
6
6
  private axiosInstance;
7
+ private isRefresh;
7
8
  constructor();
9
+ private setToken;
8
10
  private getToken;
9
11
  private removeToken;
10
12
  private refresh;
11
- logout(): void;
13
+ logoutUser(): any;
12
14
  private handleError;
13
15
  protected get<T>(url: string, options?: AxiosRequestConfig): Promise<T>;
14
16
  protected delete<T>(url: string, options?: AxiosRequestConfig): Promise<AxiosResponse<T, any, {}>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shared-ritm",
3
- "version": "1.3.43",
3
+ "version": "1.3.45",
4
4
  "private": false,
5
5
  "files": [
6
6
  "dist",
@@ -26,7 +26,7 @@ class AuthService extends ApiService {
26
26
  }
27
27
 
28
28
  public logout(): Promise<Api_Auth_Login> {
29
- return this.post<null, Api_Auth_Login>(`/v2/logout`, null)
29
+ return this.logoutUser()
30
30
  }
31
31
 
32
32
  public userInfo() {
@@ -2,7 +2,7 @@ import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, In
2
2
  import { Api_Auth_Login } from '@/api/types/Api_Auth'
3
3
 
4
4
  interface Api_Auth_Refresh {
5
- //
5
+ accessToken: string
6
6
  }
7
7
 
8
8
  export enum ApiServiceType {
@@ -12,6 +12,8 @@ export enum ApiServiceType {
12
12
  export default class ApiService {
13
13
  private axiosInstance: AxiosInstance
14
14
 
15
+ private isRefresh = false
16
+
15
17
  constructor() {
16
18
  this.axiosInstance = axios.create({
17
19
  baseURL: process.env.VUE_APP_BACKEND,
@@ -39,16 +41,30 @@ export default class ApiService {
39
41
  (response: AxiosResponse) => {
40
42
  return response.data
41
43
  },
42
- (error: AxiosError) => {
44
+ async (error: AxiosError) => {
43
45
  if (error.response?.status === 401 || error.response?.status === 403) {
44
- this.refresh()
45
- //this.logout()
46
+ if (!!this.getToken() && !this.isRefresh) {
47
+ this.isRefresh = true
48
+ return this.refresh().then(response => {
49
+ if (!response?.accessToken) {
50
+ return this.logoutUser()
51
+ }
52
+ if (response?.accessToken) {
53
+ return this.setToken(response.accessToken)
54
+ }
55
+ })
56
+ }
57
+ return this.logoutUser()
46
58
  }
47
59
  return Promise.reject(error)
48
60
  },
49
61
  )
50
62
  }
51
63
 
64
+ private setToken(token: string) {
65
+ localStorage.setItem('token', token)
66
+ }
67
+
52
68
  private getToken() {
53
69
  return localStorage.getItem('token')
54
70
  }
@@ -57,14 +73,12 @@ export default class ApiService {
57
73
  localStorage.removeItem('token')
58
74
  }
59
75
 
60
- private async refresh(): Promise<Api_Auth_Refresh> {
61
- const data = await this.post<null, Api_Auth_Login>(`/v2/auth/refresh`, null)
62
-
63
- console.log(data)
64
- return {} as any
76
+ private refresh(): Promise<Api_Auth_Refresh> {
77
+ return this.post<null, Api_Auth_Login>(`/v2/auth/refresh`, null)
65
78
  }
66
79
 
67
- public logout(): void {
80
+ public logoutUser(): any {
81
+ this.post<any, any>(`/v2/logout`, {})
68
82
  this.removeToken()
69
83
  window.location.href = '/sign-in'
70
84
  }