@cemiar/auth-sdk 1.0.21 → 1.0.23
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.
|
@@ -5,9 +5,11 @@ export declare class CemiarAuthClient implements CemiarAuthClientInstance {
|
|
|
5
5
|
private readonly tenantId;
|
|
6
6
|
private readonly auds;
|
|
7
7
|
private readonly redirectUrl?;
|
|
8
|
+
private readonly onAuthFailure?;
|
|
8
9
|
private storage;
|
|
9
10
|
private loginMethod;
|
|
10
|
-
private
|
|
11
|
+
private isRefreshing;
|
|
12
|
+
private failedQueue;
|
|
11
13
|
constructor(config: CemiarAuthClientConfig);
|
|
12
14
|
getAccessToken(): string | null;
|
|
13
15
|
setAccessToken(token: string | null): void;
|
|
@@ -21,8 +23,8 @@ export declare class CemiarAuthClient implements CemiarAuthClientInstance {
|
|
|
21
23
|
createApiClient(options: CreateApiClientOptions): AxiosInstance;
|
|
22
24
|
attachInterceptors(instance: AxiosInstance): AxiosInstance;
|
|
23
25
|
private addAuthHeader;
|
|
26
|
+
private processQueue;
|
|
24
27
|
private handleResponseError;
|
|
25
|
-
private queueTokenRefresh;
|
|
26
28
|
private loadLoginMethodFromStorage;
|
|
27
29
|
private persistLoginMethod;
|
|
28
30
|
private getCurrentLoginMethod;
|
|
@@ -17,13 +17,15 @@ export class CemiarAuthClient {
|
|
|
17
17
|
constructor(config) {
|
|
18
18
|
var _a, _b;
|
|
19
19
|
this.storage = createDefaultTokenStorage();
|
|
20
|
-
this.
|
|
20
|
+
this.isRefreshing = false;
|
|
21
|
+
this.failedQueue = [];
|
|
21
22
|
this.baseUrl = normalizeBaseUrl(config.baseUrl || "http://localhost:3000") + "/auth";
|
|
22
23
|
this.tenantId = config.tenantId;
|
|
23
24
|
this.auds = parseAuds(config.auds);
|
|
24
25
|
this.redirectUrl =
|
|
25
26
|
(_a = config.redirectUrl) !== null && _a !== void 0 ? _a : (isBrowser ? `${window.location.origin}/auth/microsoft/callback` : undefined);
|
|
26
27
|
this.storage = (_b = config.storage) !== null && _b !== void 0 ? _b : createDefaultTokenStorage();
|
|
28
|
+
this.onAuthFailure = config.onAuthFailure;
|
|
27
29
|
this.loginMethod = this.loadLoginMethodFromStorage();
|
|
28
30
|
}
|
|
29
31
|
getAccessToken() {
|
|
@@ -127,35 +129,55 @@ export class CemiarAuthClient {
|
|
|
127
129
|
}
|
|
128
130
|
return config;
|
|
129
131
|
}
|
|
132
|
+
processQueue(error, token = null) {
|
|
133
|
+
this.failedQueue.forEach(request => {
|
|
134
|
+
if (error) {
|
|
135
|
+
request.reject(error);
|
|
136
|
+
}
|
|
137
|
+
else if (token) {
|
|
138
|
+
request.resolve(token);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
this.failedQueue = [];
|
|
142
|
+
}
|
|
130
143
|
async handleResponseError(error, instance) {
|
|
131
|
-
var _a;
|
|
144
|
+
var _a, _b;
|
|
132
145
|
const status = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
133
146
|
const originalRequest = error.config;
|
|
134
147
|
if (status !== 401 || !originalRequest || originalRequest._retry) {
|
|
135
148
|
return Promise.reject(error);
|
|
136
149
|
}
|
|
150
|
+
// If already refreshing, queue this request to wait for the token
|
|
151
|
+
if (this.isRefreshing) {
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
this.failedQueue.push({ resolve, reject });
|
|
154
|
+
}).then(token => {
|
|
155
|
+
if (originalRequest.headers) {
|
|
156
|
+
originalRequest.headers.Authorization = `Bearer ${token}`;
|
|
157
|
+
}
|
|
158
|
+
return instance(originalRequest);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
137
161
|
originalRequest._retry = true;
|
|
162
|
+
this.isRefreshing = true;
|
|
138
163
|
try {
|
|
139
|
-
const newToken = await this.
|
|
164
|
+
const { accessToken: newToken } = await this.refreshToken();
|
|
165
|
+
this.processQueue(null, newToken);
|
|
140
166
|
if (originalRequest.headers) {
|
|
141
167
|
originalRequest.headers.Authorization = `Bearer ${newToken}`;
|
|
142
168
|
}
|
|
143
|
-
|
|
169
|
+
const response = await instance(originalRequest);
|
|
170
|
+
return response;
|
|
144
171
|
}
|
|
145
172
|
catch (refreshError) {
|
|
173
|
+
this.processQueue(refreshError, null);
|
|
146
174
|
this.setAccessToken(null);
|
|
175
|
+
(_b = this.onAuthFailure) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
147
176
|
return Promise.reject(refreshError);
|
|
148
177
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (!this.refreshPromise) {
|
|
152
|
-
this.refreshPromise = this.refreshToken()
|
|
153
|
-
.then(tokens => tokens.accessToken)
|
|
154
|
-
.finally(() => {
|
|
155
|
-
this.refreshPromise = null;
|
|
156
|
-
});
|
|
178
|
+
finally {
|
|
179
|
+
this.isRefreshing = false;
|
|
157
180
|
}
|
|
158
|
-
return this.refreshPromise;
|
|
159
181
|
}
|
|
160
182
|
loadLoginMethodFromStorage() {
|
|
161
183
|
if (!isBrowser || !("localStorage" in globalThis)) {
|