@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/client.js +36 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gh-platform/auth-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "VanillaJS Auth SDK for GH Platform",
5
5
  "type": "module",
6
6
  "main": "dist/auth-sdk.umd.js",
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
- const res = await fetch(this.refreshUrl, {
78
- method: "POST",
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
- let json; // πŸ‘ˆ MUST DECLARE
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
- json = await res.json();
90
- } catch (e) {
91
- console.error("❌ JSON parse error:", e);
92
- throw new Error("Invalid JSON response from server");
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) {