shared-ritm 1.3.49 → 1.3.51

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,12 +4,16 @@ export declare enum ApiServiceType {
4
4
  }
5
5
  export default class ApiService {
6
6
  private axiosInstance;
7
- private isRefresh;
7
+ private access_token;
8
+ private isRefreshing;
9
+ private refreshSubscribers;
8
10
  constructor();
9
11
  private setAccessToken;
10
12
  private getAccessToken;
11
13
  private removeToken;
14
+ private subscribeTokenRefresh;
12
15
  private refresh;
16
+ private onRefreshed;
13
17
  logoutUser(): any;
14
18
  private handleError;
15
19
  protected get<T>(url: string, options?: AxiosRequestConfig): Promise<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shared-ritm",
3
- "version": "1.3.49",
3
+ "version": "1.3.51",
4
4
  "private": false,
5
5
  "files": [
6
6
  "dist",
@@ -12,9 +12,14 @@ export enum ApiServiceType {
12
12
  export default class ApiService {
13
13
  private axiosInstance: AxiosInstance
14
14
 
15
- private isRefresh = false
15
+ private access_token: string | null = null
16
+
17
+ private isRefreshing = false
18
+ private refreshSubscribers: any[] = []
16
19
 
17
20
  constructor() {
21
+ this.access_token = this.getAccessToken()
22
+
18
23
  this.axiosInstance = axios.create({
19
24
  baseURL: process.env.VUE_APP_BACKEND,
20
25
  headers: {
@@ -26,9 +31,8 @@ export default class ApiService {
26
31
 
27
32
  this.axiosInstance.interceptors.request.use(
28
33
  (config: InternalAxiosRequestConfig) => {
29
- const access_token = this.getAccessToken()
30
- if (access_token && config.headers) {
31
- config.headers.Authorization = `Bearer ${access_token}`
34
+ if (this.access_token && config.headers) {
35
+ config.headers.Authorization = `Bearer ${this.access_token}`
32
36
  }
33
37
  return config
34
38
  },
@@ -44,26 +48,48 @@ export default class ApiService {
44
48
  async error => {
45
49
  const originalRequest = error.config
46
50
 
47
- if (originalRequest?._retry) {
48
- throw error
51
+ if (error.response?.status !== 401) {
52
+ return Promise.reject(error)
49
53
  }
50
54
 
51
- if (error.response?.status === 401 || error.response?.status === 403) {
52
- if (!this.getAccessToken()) return Promise.reject(error)
53
- if (!this.isRefresh) {
54
- originalRequest._retry = true
55
- this.isRefresh = true
56
- return this.refresh().then(response => {
57
- if (!response?.accessToken) {
58
- this.logoutUser()
59
- }
60
- if (response?.accessToken) {
61
- this.setAccessToken(response.accessToken)
62
- this.axiosInstance(originalRequest)
63
- }
55
+ if (originalRequest._retry) {
56
+ return Promise.reject(error)
57
+ }
58
+
59
+ originalRequest._retry = true
60
+
61
+ if (this.isRefreshing) {
62
+ return new Promise(resolve => {
63
+ this.subscribeTokenRefresh(token => {
64
+ originalRequest.headers['Authorization'] = `Bearer ${token}`
65
+ resolve(this.axiosInstance(originalRequest))
64
66
  })
67
+ })
68
+ }
69
+
70
+ this.isRefreshing = true
71
+
72
+ try {
73
+ const response = await this.refresh()
74
+
75
+ const newToken = response.accessToken
76
+
77
+ if (!newToken) {
78
+ this.logoutUser()
79
+ return Promise.reject(error)
65
80
  }
66
- return this.logoutUser()
81
+
82
+ this.setAccessToken(newToken)
83
+
84
+ this.onRefreshed(newToken)
85
+ this.isRefreshing = false
86
+
87
+ originalRequest.headers['Authorization'] = `Bearer ${newToken}`
88
+ return this.axiosInstance(originalRequest)
89
+ } catch (e) {
90
+ this.isRefreshing = false
91
+ this.logoutUser()
92
+ return Promise.reject(e)
67
93
  }
68
94
  },
69
95
  )
@@ -80,10 +106,19 @@ export default class ApiService {
80
106
  localStorage.removeItem('token')
81
107
  }
82
108
 
109
+ private subscribeTokenRefresh(cb: any) {
110
+ this.refreshSubscribers.push(cb)
111
+ }
112
+
83
113
  private refresh(): Promise<Api_Auth_Refresh> {
84
114
  return this.post<null, Api_Auth_Login>(`/v2/auth/refresh`, null)
85
115
  }
86
116
 
117
+ private onRefreshed(token: string) {
118
+ this.refreshSubscribers.forEach(cb => cb(token))
119
+ this.refreshSubscribers = []
120
+ }
121
+
87
122
  public logoutUser(): any {
88
123
  this.post<any, any>(`/v2/logout`, {})
89
124
  this.removeToken()