@gh-platform/auth-sdk 1.0.0 β 1.0.1
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.
- package/package.json +1 -1
- package/src/client.js +36 -24
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -7,6 +7,7 @@ export default class AuthClient {
|
|
|
7
7
|
* - loginPath / refreshPath / introspectPath (optional overrides)
|
|
8
8
|
* - headers (optional)
|
|
9
9
|
*/
|
|
10
|
+
_refreshPromise = null;
|
|
10
11
|
constructor({
|
|
11
12
|
baseUrl,
|
|
12
13
|
tenant = null,
|
|
@@ -74,33 +75,44 @@ export default class AuthClient {
|
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
async refresh(refreshToken) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
headers: this.headers,
|
|
80
|
-
body: JSON.stringify({ refresh_token: refreshToken }),
|
|
81
|
-
});
|
|
82
|
-
if (!res.ok) {
|
|
83
|
-
const text = await res.text().catch(() => res.statusText);
|
|
84
|
-
throw new Error(`Refresh failed: ${res.status} ${text}`);
|
|
78
|
+
if (this._refreshPromise) {
|
|
79
|
+
return this._refreshPromise;
|
|
85
80
|
}
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
this._refreshPromise = (async () => {
|
|
82
|
+
const res = await fetch(this.refreshUrl, {
|
|
83
|
+
method: "POST",
|
|
84
|
+
headers: this.headers,
|
|
85
|
+
body: JSON.stringify({ refresh_token: refreshToken }),
|
|
86
|
+
});
|
|
87
|
+
if (!res.ok) {
|
|
88
|
+
const text = await res.text().catch(() => res.statusText);
|
|
89
|
+
throw new Error(`Refresh failed: ${res.status} ${text}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let json; // π MUST DECLARE
|
|
93
|
+
try {
|
|
94
|
+
json = await res.json();
|
|
95
|
+
} catch (e) {
|
|
96
|
+
console.error("β JSON parse error:", e);
|
|
97
|
+
throw new Error("Invalid JSON response from server");
|
|
98
|
+
}
|
|
99
|
+
const data = json.data || json;
|
|
100
|
+
// -----------------------------
|
|
101
|
+
// π₯ Auto-save refreshed tokens
|
|
102
|
+
// -----------------------------
|
|
103
|
+
if (this.storage) {
|
|
104
|
+
if (data.access_token) this.storage.accessToken = data.access_token;
|
|
105
|
+
if (data.refresh_token) this.storage.refreshToken = data.refresh_token;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return json;
|
|
109
|
+
})();
|
|
88
110
|
try {
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
const data = json.data || json;
|
|
95
|
-
// -----------------------------
|
|
96
|
-
// π₯ Auto-save refreshed tokens
|
|
97
|
-
// -----------------------------
|
|
98
|
-
if (this.storage) {
|
|
99
|
-
if (data.access_token) this.storage.accessToken = data.access_token;
|
|
100
|
-
if (data.refresh_token) this.storage.refreshToken = data.refresh_token;
|
|
111
|
+
return await this._refreshPromise;
|
|
112
|
+
} finally {
|
|
113
|
+
// π LuΓ΄n reset lock (kα» cαΊ£ khi lα»i)
|
|
114
|
+
this._refreshPromise = null;
|
|
101
115
|
}
|
|
102
|
-
|
|
103
|
-
return json;
|
|
104
116
|
}
|
|
105
117
|
|
|
106
118
|
async introspect(token = null) {
|