@insforge/sdk 1.0.1-refresh.6 → 1.0.1-refresh.8
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/dist/index.d.mts +46 -169
- package/dist/index.d.ts +46 -169
- package/dist/index.js +170 -398
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +170 -396
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -21,8 +21,6 @@ var InsForgeError = class _InsForgeError extends Error {
|
|
|
21
21
|
var HttpClient = class {
|
|
22
22
|
constructor(config) {
|
|
23
23
|
this.userToken = null;
|
|
24
|
-
this.isRefreshing = false;
|
|
25
|
-
this.refreshQueue = [];
|
|
26
24
|
this.baseUrl = config.baseUrl || "http://localhost:7130";
|
|
27
25
|
this.fetch = config.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
|
|
28
26
|
this.anonKey = config.anonKey;
|
|
@@ -35,12 +33,6 @@ var HttpClient = class {
|
|
|
35
33
|
);
|
|
36
34
|
}
|
|
37
35
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Set the refresh callback for automatic token refresh on 401
|
|
40
|
-
*/
|
|
41
|
-
setRefreshCallback(callback) {
|
|
42
|
-
this.refreshCallback = callback;
|
|
43
|
-
}
|
|
44
36
|
buildUrl(path, params) {
|
|
45
37
|
const url = new URL(path, this.baseUrl);
|
|
46
38
|
if (params) {
|
|
@@ -57,9 +49,6 @@ var HttpClient = class {
|
|
|
57
49
|
return url.toString();
|
|
58
50
|
}
|
|
59
51
|
async request(method, path, options = {}) {
|
|
60
|
-
return this.performRequest(method, path, options, false);
|
|
61
|
-
}
|
|
62
|
-
async performRequest(method, path, options = {}, isRetry = false) {
|
|
63
52
|
const { params, headers = {}, body, ...fetchOptions } = options;
|
|
64
53
|
const url = this.buildUrl(path, params);
|
|
65
54
|
const requestHeaders = {
|
|
@@ -85,17 +74,9 @@ var HttpClient = class {
|
|
|
85
74
|
method,
|
|
86
75
|
headers: requestHeaders,
|
|
87
76
|
body: processedBody,
|
|
88
|
-
|
|
89
|
-
|
|
77
|
+
credentials: "include",
|
|
78
|
+
...fetchOptions
|
|
90
79
|
});
|
|
91
|
-
const isRefreshEndpoint = path.includes("/api/auth/refresh") || path.includes("/api/auth/logout");
|
|
92
|
-
if (response.status === 401 && !isRetry && !isRefreshEndpoint && this.refreshCallback) {
|
|
93
|
-
const newToken = await this.handleTokenRefresh();
|
|
94
|
-
if (newToken) {
|
|
95
|
-
this.setAuthToken(newToken);
|
|
96
|
-
return this.performRequest(method, path, options, true);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
80
|
if (response.status === 204) {
|
|
100
81
|
return void 0;
|
|
101
82
|
}
|
|
@@ -127,38 +108,6 @@ var HttpClient = class {
|
|
|
127
108
|
}
|
|
128
109
|
return data;
|
|
129
110
|
}
|
|
130
|
-
/**
|
|
131
|
-
* Handle token refresh with queue to prevent duplicate refreshes
|
|
132
|
-
* Multiple concurrent 401s will wait for a single refresh to complete
|
|
133
|
-
*/
|
|
134
|
-
async handleTokenRefresh() {
|
|
135
|
-
if (this.isRefreshing) {
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
this.refreshQueue.push({ resolve, reject });
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
this.isRefreshing = true;
|
|
141
|
-
try {
|
|
142
|
-
const newToken = await this.refreshCallback?.();
|
|
143
|
-
this.refreshQueue.forEach(({ resolve, reject }) => {
|
|
144
|
-
if (newToken) {
|
|
145
|
-
resolve(newToken);
|
|
146
|
-
} else {
|
|
147
|
-
reject(new Error("Token refresh failed"));
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
this.refreshQueue = [];
|
|
151
|
-
return newToken || null;
|
|
152
|
-
} catch (error) {
|
|
153
|
-
this.refreshQueue.forEach(({ reject }) => {
|
|
154
|
-
reject(error instanceof Error ? error : new Error("Token refresh failed"));
|
|
155
|
-
});
|
|
156
|
-
this.refreshQueue = [];
|
|
157
|
-
return null;
|
|
158
|
-
} finally {
|
|
159
|
-
this.isRefreshing = false;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
111
|
get(path, options) {
|
|
163
112
|
return this.request("GET", path, options);
|
|
164
113
|
}
|
|
@@ -187,58 +136,32 @@ var HttpClient = class {
|
|
|
187
136
|
}
|
|
188
137
|
};
|
|
189
138
|
|
|
190
|
-
// src/lib/
|
|
139
|
+
// src/lib/token-manager.ts
|
|
191
140
|
var TOKEN_KEY = "insforge-auth-token";
|
|
192
141
|
var USER_KEY = "insforge-auth-user";
|
|
193
142
|
var AUTH_FLAG_COOKIE = "isAuthenticated";
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
return this.accessToken;
|
|
213
|
-
}
|
|
214
|
-
setAccessToken(token) {
|
|
215
|
-
this.accessToken = token;
|
|
216
|
-
}
|
|
217
|
-
getUser() {
|
|
218
|
-
return this.user;
|
|
219
|
-
}
|
|
220
|
-
setUser(user) {
|
|
221
|
-
this.user = user;
|
|
222
|
-
}
|
|
223
|
-
clearSession() {
|
|
143
|
+
function hasAuthCookie() {
|
|
144
|
+
if (typeof document === "undefined") return false;
|
|
145
|
+
return document.cookie.split(";").some(
|
|
146
|
+
(c) => c.trim().startsWith(`${AUTH_FLAG_COOKIE}=`)
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
function setAuthCookie() {
|
|
150
|
+
if (typeof document === "undefined") return;
|
|
151
|
+
const maxAge = 7 * 24 * 60 * 60;
|
|
152
|
+
document.cookie = `${AUTH_FLAG_COOKIE}=true; path=/; max-age=${maxAge}; SameSite=Lax`;
|
|
153
|
+
}
|
|
154
|
+
function clearAuthCookie() {
|
|
155
|
+
if (typeof document === "undefined") return;
|
|
156
|
+
document.cookie = `${AUTH_FLAG_COOKIE}=; path=/; max-age=0; SameSite=Lax`;
|
|
157
|
+
}
|
|
158
|
+
var TokenManager = class {
|
|
159
|
+
constructor(storage) {
|
|
160
|
+
// In-memory storage
|
|
224
161
|
this.accessToken = null;
|
|
225
162
|
this.user = null;
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (this.accessToken) return false;
|
|
229
|
-
return this.hasAuthFlag();
|
|
230
|
-
}
|
|
231
|
-
// --- Private: Auth Flag Cookie Detection (SDK-managed on frontend domain) ---
|
|
232
|
-
hasAuthFlag() {
|
|
233
|
-
if (typeof document === "undefined") return false;
|
|
234
|
-
return document.cookie.split(";").some(
|
|
235
|
-
(c) => c.trim().startsWith(`${AUTH_FLAG_COOKIE}=`)
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
var LocalSessionStorage = class {
|
|
240
|
-
constructor(storage) {
|
|
241
|
-
this.strategyId = "local";
|
|
163
|
+
// Mode: 'memory' (new backend) or 'storage' (legacy backend, default)
|
|
164
|
+
this._mode = "storage";
|
|
242
165
|
if (storage) {
|
|
243
166
|
this.storage = storage;
|
|
244
167
|
} else if (typeof window !== "undefined" && window.localStorage) {
|
|
@@ -256,126 +179,111 @@ var LocalSessionStorage = class {
|
|
|
256
179
|
};
|
|
257
180
|
}
|
|
258
181
|
}
|
|
259
|
-
saveSession(session) {
|
|
260
|
-
this.storage.setItem(TOKEN_KEY, session.accessToken);
|
|
261
|
-
this.storage.setItem(USER_KEY, JSON.stringify(session.user));
|
|
262
|
-
}
|
|
263
|
-
getSession() {
|
|
264
|
-
const token = this.storage.getItem(TOKEN_KEY);
|
|
265
|
-
const userStr = this.storage.getItem(USER_KEY);
|
|
266
|
-
if (!token || !userStr) return null;
|
|
267
|
-
try {
|
|
268
|
-
const user = JSON.parse(userStr);
|
|
269
|
-
return { accessToken: token, user };
|
|
270
|
-
} catch {
|
|
271
|
-
this.clearSession();
|
|
272
|
-
return null;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
getAccessToken() {
|
|
276
|
-
const token = this.storage.getItem(TOKEN_KEY);
|
|
277
|
-
return typeof token === "string" ? token : null;
|
|
278
|
-
}
|
|
279
|
-
setAccessToken(token) {
|
|
280
|
-
this.storage.setItem(TOKEN_KEY, token);
|
|
281
|
-
}
|
|
282
|
-
getUser() {
|
|
283
|
-
const userStr = this.storage.getItem(USER_KEY);
|
|
284
|
-
if (!userStr) return null;
|
|
285
|
-
try {
|
|
286
|
-
return JSON.parse(userStr);
|
|
287
|
-
} catch {
|
|
288
|
-
return null;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
setUser(user) {
|
|
292
|
-
this.storage.setItem(USER_KEY, JSON.stringify(user));
|
|
293
|
-
}
|
|
294
|
-
clearSession() {
|
|
295
|
-
this.storage.removeItem(TOKEN_KEY);
|
|
296
|
-
this.storage.removeItem(USER_KEY);
|
|
297
|
-
}
|
|
298
|
-
shouldAttemptRefresh() {
|
|
299
|
-
return false;
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
// src/lib/token-manager.ts
|
|
304
|
-
var TokenManager = class {
|
|
305
182
|
/**
|
|
306
|
-
*
|
|
307
|
-
* @param storage - Optional custom storage adapter (used for initial LocalSessionStorage)
|
|
183
|
+
* Get current mode
|
|
308
184
|
*/
|
|
309
|
-
|
|
310
|
-
this.
|
|
185
|
+
get mode() {
|
|
186
|
+
return this._mode;
|
|
311
187
|
}
|
|
312
188
|
/**
|
|
313
|
-
* Set
|
|
314
|
-
* Called after capability discovery to switch to the appropriate strategy
|
|
189
|
+
* Set mode to memory (new backend with cookies + memory)
|
|
315
190
|
*/
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
if (existingSession && previousId !== strategy.strategyId) {
|
|
321
|
-
strategy.saveSession(existingSession);
|
|
191
|
+
setMemoryMode() {
|
|
192
|
+
if (this._mode === "storage") {
|
|
193
|
+
this.storage.removeItem(TOKEN_KEY);
|
|
194
|
+
this.storage.removeItem(USER_KEY);
|
|
322
195
|
}
|
|
196
|
+
this._mode = "memory";
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Set mode to storage (legacy backend with localStorage)
|
|
200
|
+
* Also loads existing session from localStorage
|
|
201
|
+
*/
|
|
202
|
+
setStorageMode() {
|
|
203
|
+
this._mode = "storage";
|
|
204
|
+
this.loadFromStorage();
|
|
323
205
|
}
|
|
324
206
|
/**
|
|
325
|
-
*
|
|
207
|
+
* Load session from localStorage
|
|
326
208
|
*/
|
|
327
|
-
|
|
328
|
-
|
|
209
|
+
loadFromStorage() {
|
|
210
|
+
const token = this.storage.getItem(TOKEN_KEY);
|
|
211
|
+
const userStr = this.storage.getItem(USER_KEY);
|
|
212
|
+
if (token && userStr) {
|
|
213
|
+
try {
|
|
214
|
+
this.accessToken = token;
|
|
215
|
+
this.user = JSON.parse(userStr);
|
|
216
|
+
} catch {
|
|
217
|
+
this.clearSession();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
329
220
|
}
|
|
330
|
-
// --- Delegated Methods ---
|
|
331
221
|
/**
|
|
332
|
-
* Save session
|
|
222
|
+
* Save session (memory always, localStorage only in storage mode)
|
|
333
223
|
*/
|
|
334
224
|
saveSession(session) {
|
|
335
|
-
this.
|
|
225
|
+
this.accessToken = session.accessToken;
|
|
226
|
+
this.user = session.user;
|
|
227
|
+
if (this._mode === "storage") {
|
|
228
|
+
this.storage.setItem(TOKEN_KEY, session.accessToken);
|
|
229
|
+
this.storage.setItem(USER_KEY, JSON.stringify(session.user));
|
|
230
|
+
}
|
|
336
231
|
}
|
|
337
232
|
/**
|
|
338
233
|
* Get current session
|
|
339
234
|
*/
|
|
340
235
|
getSession() {
|
|
341
|
-
|
|
236
|
+
if (!this.accessToken || !this.user) return null;
|
|
237
|
+
return {
|
|
238
|
+
accessToken: this.accessToken,
|
|
239
|
+
user: this.user
|
|
240
|
+
};
|
|
342
241
|
}
|
|
343
242
|
/**
|
|
344
243
|
* Get access token
|
|
345
244
|
*/
|
|
346
245
|
getAccessToken() {
|
|
347
|
-
return this.
|
|
246
|
+
return this.accessToken;
|
|
348
247
|
}
|
|
349
248
|
/**
|
|
350
|
-
*
|
|
249
|
+
* Set access token
|
|
351
250
|
*/
|
|
352
251
|
setAccessToken(token) {
|
|
353
|
-
this.
|
|
252
|
+
this.accessToken = token;
|
|
253
|
+
if (this._mode === "storage") {
|
|
254
|
+
this.storage.setItem(TOKEN_KEY, token);
|
|
255
|
+
}
|
|
354
256
|
}
|
|
355
257
|
/**
|
|
356
|
-
* Get user
|
|
258
|
+
* Get user
|
|
357
259
|
*/
|
|
358
260
|
getUser() {
|
|
359
|
-
return this.
|
|
261
|
+
return this.user;
|
|
360
262
|
}
|
|
361
263
|
/**
|
|
362
|
-
*
|
|
264
|
+
* Set user
|
|
363
265
|
*/
|
|
364
266
|
setUser(user) {
|
|
365
|
-
this.
|
|
267
|
+
this.user = user;
|
|
268
|
+
if (this._mode === "storage") {
|
|
269
|
+
this.storage.setItem(USER_KEY, JSON.stringify(user));
|
|
270
|
+
}
|
|
366
271
|
}
|
|
367
272
|
/**
|
|
368
|
-
* Clear
|
|
273
|
+
* Clear session (both memory and localStorage)
|
|
369
274
|
*/
|
|
370
275
|
clearSession() {
|
|
371
|
-
this.
|
|
276
|
+
this.accessToken = null;
|
|
277
|
+
this.user = null;
|
|
278
|
+
this.storage.removeItem(TOKEN_KEY);
|
|
279
|
+
this.storage.removeItem(USER_KEY);
|
|
372
280
|
}
|
|
373
281
|
/**
|
|
374
|
-
* Check if
|
|
375
|
-
* (e.g., on page reload in secure mode)
|
|
282
|
+
* Check if there's a session in localStorage (for legacy detection)
|
|
376
283
|
*/
|
|
377
|
-
|
|
378
|
-
|
|
284
|
+
hasStoredSession() {
|
|
285
|
+
const token = this.storage.getItem(TOKEN_KEY);
|
|
286
|
+
return !!token;
|
|
379
287
|
}
|
|
380
288
|
};
|
|
381
289
|
|
|
@@ -492,75 +400,75 @@ var Auth = class {
|
|
|
492
400
|
this.detectAuthCallback();
|
|
493
401
|
}
|
|
494
402
|
/**
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
* Called when backend returns sessionMode: 'secure'
|
|
513
|
-
* @internal
|
|
514
|
-
*/
|
|
515
|
-
_switchToSecureStorage() {
|
|
516
|
-
console.log("[InsForge:Auth] _switchToSecureStorage() called, current strategy:", this.tokenManager.getStrategyId());
|
|
517
|
-
if (this.tokenManager.getStrategyId() === "secure") {
|
|
518
|
-
console.log("[InsForge:Auth] _switchToSecureStorage() - already in secure mode, skipping");
|
|
519
|
-
return;
|
|
403
|
+
* Restore session on app initialization
|
|
404
|
+
*
|
|
405
|
+
* @returns Object with isLoggedIn status
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* ```typescript
|
|
409
|
+
* const client = new InsForgeClient({ baseUrl: '...' });
|
|
410
|
+
* const { isLoggedIn } = await client.auth.restoreSession();
|
|
411
|
+
*
|
|
412
|
+
* if (isLoggedIn) {
|
|
413
|
+
* const { data } = await client.auth.getCurrentUser();
|
|
414
|
+
* }
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
async restoreSession() {
|
|
418
|
+
if (typeof window === "undefined") {
|
|
419
|
+
return { isLoggedIn: false };
|
|
520
420
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
if (typeof localStorage !== "undefined") {
|
|
524
|
-
console.log("[InsForge:Auth] _switchToSecureStorage() - clearing localStorage");
|
|
525
|
-
localStorage.removeItem(TOKEN_KEY);
|
|
526
|
-
localStorage.removeItem(USER_KEY);
|
|
421
|
+
if (this.tokenManager.getAccessToken()) {
|
|
422
|
+
return { isLoggedIn: true };
|
|
527
423
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
424
|
+
if (hasAuthCookie()) {
|
|
425
|
+
try {
|
|
426
|
+
const response = await this.http.post(
|
|
427
|
+
"/api/auth/refresh"
|
|
428
|
+
);
|
|
429
|
+
if (response.accessToken) {
|
|
430
|
+
this.tokenManager.setMemoryMode();
|
|
431
|
+
this.tokenManager.setAccessToken(response.accessToken);
|
|
432
|
+
this.http.setAuthToken(response.accessToken);
|
|
433
|
+
if (response.user) {
|
|
434
|
+
this.tokenManager.setUser(response.user);
|
|
435
|
+
}
|
|
436
|
+
return { isLoggedIn: true };
|
|
437
|
+
}
|
|
438
|
+
} catch (error) {
|
|
439
|
+
if (error instanceof InsForgeError) {
|
|
440
|
+
if (error.statusCode === 404) {
|
|
441
|
+
this.tokenManager.setStorageMode();
|
|
442
|
+
const token = this.tokenManager.getAccessToken();
|
|
443
|
+
if (token) {
|
|
444
|
+
this.http.setAuthToken(token);
|
|
445
|
+
return { isLoggedIn: true };
|
|
446
|
+
}
|
|
447
|
+
return { isLoggedIn: false };
|
|
448
|
+
}
|
|
449
|
+
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
450
|
+
clearAuthCookie();
|
|
451
|
+
return { isLoggedIn: false };
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return { isLoggedIn: false };
|
|
455
|
+
}
|
|
532
456
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
if (this.tokenManager.getStrategyId() === "local") return;
|
|
541
|
-
const currentSession = this.tokenManager.getSession();
|
|
542
|
-
this.tokenManager.setStrategy(new LocalSessionStorage());
|
|
543
|
-
this.clearAuthenticatedCookie();
|
|
544
|
-
if (currentSession) {
|
|
545
|
-
this.tokenManager.saveSession(currentSession);
|
|
457
|
+
if (this.tokenManager.hasStoredSession()) {
|
|
458
|
+
this.tokenManager.setStorageMode();
|
|
459
|
+
const token = this.tokenManager.getAccessToken();
|
|
460
|
+
if (token) {
|
|
461
|
+
this.http.setAuthToken(token);
|
|
462
|
+
return { isLoggedIn: true };
|
|
463
|
+
}
|
|
546
464
|
}
|
|
465
|
+
return { isLoggedIn: false };
|
|
547
466
|
}
|
|
548
467
|
/**
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
468
|
+
* Automatically detect and handle OAuth callback parameters in the URL
|
|
469
|
+
* This runs on initialization to seamlessly complete the OAuth flow
|
|
470
|
+
* Matches the backend's OAuth callback response (backend/src/api/routes/auth.ts:540-544)
|
|
552
471
|
*/
|
|
553
|
-
_detectStorageFromResponse(sessionMode) {
|
|
554
|
-
console.log("[InsForge:Auth] _detectStorageFromResponse() - sessionMode:", sessionMode);
|
|
555
|
-
if (sessionMode === "secure") {
|
|
556
|
-
this._switchToSecureStorage();
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* Automatically detect and handle OAuth callback parameters in the URL
|
|
561
|
-
* This runs on initialization to seamlessly complete the OAuth flow
|
|
562
|
-
* Matches the backend's OAuth callback response (backend/src/api/routes/auth.ts:540-544)
|
|
563
|
-
*/
|
|
564
472
|
detectAuthCallback() {
|
|
565
473
|
if (typeof window === "undefined") return;
|
|
566
474
|
try {
|
|
@@ -569,9 +477,7 @@ var Auth = class {
|
|
|
569
477
|
const userId = params.get("user_id");
|
|
570
478
|
const email = params.get("email");
|
|
571
479
|
const name = params.get("name");
|
|
572
|
-
const sessionMode = params.get("session_mode");
|
|
573
480
|
if (accessToken && userId && email) {
|
|
574
|
-
this._detectStorageFromResponse(sessionMode || void 0);
|
|
575
481
|
const session = {
|
|
576
482
|
accessToken,
|
|
577
483
|
user: {
|
|
@@ -585,14 +491,14 @@ var Auth = class {
|
|
|
585
491
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
586
492
|
}
|
|
587
493
|
};
|
|
588
|
-
this.tokenManager.saveSession(session);
|
|
589
494
|
this.http.setAuthToken(accessToken);
|
|
495
|
+
this.tokenManager.saveSession(session);
|
|
496
|
+
setAuthCookie();
|
|
590
497
|
const url = new URL(window.location.href);
|
|
591
498
|
url.searchParams.delete("access_token");
|
|
592
499
|
url.searchParams.delete("user_id");
|
|
593
500
|
url.searchParams.delete("email");
|
|
594
501
|
url.searchParams.delete("name");
|
|
595
|
-
url.searchParams.delete("session_mode");
|
|
596
502
|
if (params.has("error")) {
|
|
597
503
|
url.searchParams.delete("error");
|
|
598
504
|
}
|
|
@@ -608,16 +514,13 @@ var Auth = class {
|
|
|
608
514
|
async signUp(request) {
|
|
609
515
|
try {
|
|
610
516
|
const response = await this.http.post("/api/auth/users", request);
|
|
611
|
-
|
|
612
|
-
this._detectStorageFromResponse(sessionMode);
|
|
613
|
-
if (response.accessToken && response.user) {
|
|
517
|
+
if (response.accessToken && response.user && !isHostedAuthEnvironment()) {
|
|
614
518
|
const session = {
|
|
615
519
|
accessToken: response.accessToken,
|
|
616
520
|
user: response.user
|
|
617
521
|
};
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
}
|
|
522
|
+
this.tokenManager.saveSession(session);
|
|
523
|
+
setAuthCookie();
|
|
621
524
|
this.http.setAuthToken(response.accessToken);
|
|
622
525
|
}
|
|
623
526
|
return {
|
|
@@ -644,23 +547,15 @@ var Auth = class {
|
|
|
644
547
|
async signInWithPassword(request) {
|
|
645
548
|
try {
|
|
646
549
|
const response = await this.http.post("/api/auth/sessions", request);
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
id: "",
|
|
653
|
-
email: "",
|
|
654
|
-
name: "",
|
|
655
|
-
emailVerified: false,
|
|
656
|
-
createdAt: "",
|
|
657
|
-
updatedAt: ""
|
|
658
|
-
}
|
|
659
|
-
};
|
|
660
|
-
if (!isHostedAuthEnvironment()) {
|
|
550
|
+
if (response.accessToken && response.user && !isHostedAuthEnvironment()) {
|
|
551
|
+
const session = {
|
|
552
|
+
accessToken: response.accessToken,
|
|
553
|
+
user: response.user
|
|
554
|
+
};
|
|
661
555
|
this.tokenManager.saveSession(session);
|
|
556
|
+
setAuthCookie();
|
|
557
|
+
this.http.setAuthToken(response.accessToken);
|
|
662
558
|
}
|
|
663
|
-
this.http.setAuthToken(response.accessToken || "");
|
|
664
559
|
return {
|
|
665
560
|
data: response,
|
|
666
561
|
error: null
|
|
@@ -715,28 +610,18 @@ var Auth = class {
|
|
|
715
610
|
}
|
|
716
611
|
/**
|
|
717
612
|
* Sign out the current user
|
|
718
|
-
* In modern mode, also calls backend to clear the refresh token cookie
|
|
719
613
|
*/
|
|
720
614
|
async signOut() {
|
|
721
|
-
console.log("[InsForge:Auth] signOut() called");
|
|
722
|
-
console.log("[InsForge:Auth] signOut() stack trace:", new Error().stack);
|
|
723
615
|
try {
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
await this.http.post("/api/auth/logout");
|
|
728
|
-
console.log("[InsForge:Auth] signOut() - backend logout successful");
|
|
729
|
-
} catch (e) {
|
|
730
|
-
console.log("[InsForge:Auth] signOut() - backend logout failed (ignored):", e);
|
|
731
|
-
}
|
|
616
|
+
try {
|
|
617
|
+
await this.http.post("/api/auth/logout");
|
|
618
|
+
} catch {
|
|
732
619
|
}
|
|
733
620
|
this.tokenManager.clearSession();
|
|
734
621
|
this.http.setAuthToken(null);
|
|
735
|
-
|
|
736
|
-
console.log("[InsForge:Auth] signOut() - completed");
|
|
622
|
+
clearAuthCookie();
|
|
737
623
|
return { error: null };
|
|
738
624
|
} catch (error) {
|
|
739
|
-
console.error("[InsForge:Auth] signOut() - error:", error);
|
|
740
625
|
return {
|
|
741
626
|
error: new InsForgeError(
|
|
742
627
|
"Failed to sign out",
|
|
@@ -746,52 +631,6 @@ var Auth = class {
|
|
|
746
631
|
};
|
|
747
632
|
}
|
|
748
633
|
}
|
|
749
|
-
/**
|
|
750
|
-
* Refresh the access token using the httpOnly refresh token cookie
|
|
751
|
-
* Only works when backend supports secure session storage (httpOnly cookies)
|
|
752
|
-
*
|
|
753
|
-
* @returns New access token or throws an error
|
|
754
|
-
*/
|
|
755
|
-
async refreshToken() {
|
|
756
|
-
console.log("[InsForge:Auth] refreshToken() called");
|
|
757
|
-
try {
|
|
758
|
-
const response = await this.http.post(
|
|
759
|
-
"/api/auth/refresh"
|
|
760
|
-
);
|
|
761
|
-
console.log("[InsForge:Auth] refreshToken() - response received, hasAccessToken:", !!response.accessToken);
|
|
762
|
-
if (response.accessToken) {
|
|
763
|
-
this._detectStorageFromResponse(response.sessionMode);
|
|
764
|
-
this.tokenManager.setAccessToken(response.accessToken);
|
|
765
|
-
this.http.setAuthToken(response.accessToken);
|
|
766
|
-
if (response.user) {
|
|
767
|
-
this.tokenManager.setUser(response.user);
|
|
768
|
-
}
|
|
769
|
-
console.log("[InsForge:Auth] refreshToken() - success");
|
|
770
|
-
return response.accessToken;
|
|
771
|
-
}
|
|
772
|
-
throw new InsForgeError(
|
|
773
|
-
"No access token in refresh response",
|
|
774
|
-
500,
|
|
775
|
-
"REFRESH_FAILED"
|
|
776
|
-
);
|
|
777
|
-
} catch (error) {
|
|
778
|
-
console.error("[InsForge:Auth] refreshToken() - error:", error);
|
|
779
|
-
if (error instanceof InsForgeError) {
|
|
780
|
-
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
781
|
-
console.log("[InsForge:Auth] refreshToken() - clearing session due to 401/403");
|
|
782
|
-
this.tokenManager.clearSession();
|
|
783
|
-
this.http.setAuthToken(null);
|
|
784
|
-
this.clearAuthenticatedCookie();
|
|
785
|
-
}
|
|
786
|
-
throw error;
|
|
787
|
-
}
|
|
788
|
-
throw new InsForgeError(
|
|
789
|
-
"Token refresh failed",
|
|
790
|
-
500,
|
|
791
|
-
"REFRESH_FAILED"
|
|
792
|
-
);
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
634
|
/**
|
|
796
635
|
* Get all public authentication configuration (OAuth + Email)
|
|
797
636
|
* Returns both OAuth providers and email authentication settings in one request
|
|
@@ -832,40 +671,19 @@ var Auth = class {
|
|
|
832
671
|
/**
|
|
833
672
|
* Get the current user with full profile information
|
|
834
673
|
* Returns both auth info (id, email, role) and profile data (dynamic fields from users table)
|
|
835
|
-
*
|
|
836
|
-
* In secure session mode (httpOnly cookie), this method will automatically attempt
|
|
837
|
-
* to refresh the session if no access token is available (e.g., after page reload).
|
|
838
674
|
*/
|
|
839
675
|
async getCurrentUser() {
|
|
840
|
-
console.log("[InsForge:Auth] getCurrentUser() called");
|
|
841
676
|
try {
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
console.log("[InsForge:Auth] getCurrentUser() - hasAccessToken:", !!accessToken, "shouldAttemptRefresh:", shouldRefresh);
|
|
845
|
-
if (!accessToken && shouldRefresh) {
|
|
846
|
-
console.log("[InsForge:Auth] getCurrentUser() - attempting refresh");
|
|
847
|
-
try {
|
|
848
|
-
accessToken = await this.refreshToken();
|
|
849
|
-
} catch (error) {
|
|
850
|
-
console.log("[InsForge:Auth] getCurrentUser() - refresh failed:", error);
|
|
851
|
-
if (error instanceof InsForgeError && (error.statusCode === 401 || error.statusCode === 403)) {
|
|
852
|
-
return { data: null, error };
|
|
853
|
-
}
|
|
854
|
-
return { data: null, error: error instanceof InsForgeError ? error : new InsForgeError("Token refresh failed", 500, "REFRESH_FAILED") };
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
if (!accessToken) {
|
|
858
|
-
console.log("[InsForge:Auth] getCurrentUser() - no access token, returning null");
|
|
677
|
+
const session = this.tokenManager.getSession();
|
|
678
|
+
if (!session?.accessToken) {
|
|
859
679
|
return { data: null, error: null };
|
|
860
680
|
}
|
|
861
|
-
this.http.setAuthToken(accessToken);
|
|
862
|
-
console.log("[InsForge:Auth] getCurrentUser() - fetching user from API");
|
|
681
|
+
this.http.setAuthToken(session.accessToken);
|
|
863
682
|
const authResponse = await this.http.get("/api/auth/sessions/current");
|
|
864
683
|
const { data: profile, error: profileError } = await this.database.from("users").select("*").eq("id", authResponse.user.id).single();
|
|
865
684
|
if (profileError && profileError.code !== "PGRST116") {
|
|
866
685
|
return { data: null, error: profileError };
|
|
867
686
|
}
|
|
868
|
-
console.log("[InsForge:Auth] getCurrentUser() - success");
|
|
869
687
|
return {
|
|
870
688
|
data: {
|
|
871
689
|
user: authResponse.user,
|
|
@@ -874,12 +692,8 @@ var Auth = class {
|
|
|
874
692
|
error: null
|
|
875
693
|
};
|
|
876
694
|
} catch (error) {
|
|
877
|
-
console.error("[InsForge:Auth] getCurrentUser() - catch error:", error);
|
|
878
695
|
if (error instanceof InsForgeError && error.statusCode === 401) {
|
|
879
|
-
|
|
880
|
-
this.tokenManager.clearSession();
|
|
881
|
-
this.http.setAuthToken(null);
|
|
882
|
-
this.clearAuthenticatedCookie();
|
|
696
|
+
await this.signOut();
|
|
883
697
|
return { data: null, error: null };
|
|
884
698
|
}
|
|
885
699
|
if (error instanceof InsForgeError) {
|
|
@@ -1127,15 +941,14 @@ var Auth = class {
|
|
|
1127
941
|
"/api/auth/email/verify",
|
|
1128
942
|
request
|
|
1129
943
|
);
|
|
1130
|
-
|
|
1131
|
-
this._detectStorageFromResponse(sessionMode);
|
|
1132
|
-
if (response.accessToken) {
|
|
944
|
+
if (response.accessToken && !isHostedAuthEnvironment()) {
|
|
1133
945
|
const session = {
|
|
1134
946
|
accessToken: response.accessToken,
|
|
1135
947
|
user: response.user || {}
|
|
1136
948
|
};
|
|
1137
949
|
this.tokenManager.saveSession(session);
|
|
1138
950
|
this.http.setAuthToken(response.accessToken);
|
|
951
|
+
setAuthCookie();
|
|
1139
952
|
}
|
|
1140
953
|
return {
|
|
1141
954
|
data: response,
|
|
@@ -1679,24 +1492,10 @@ var Functions = class {
|
|
|
1679
1492
|
};
|
|
1680
1493
|
|
|
1681
1494
|
// src/client.ts
|
|
1682
|
-
function hasAuthenticatedCookie() {
|
|
1683
|
-
if (typeof document === "undefined") return false;
|
|
1684
|
-
return document.cookie.split(";").some(
|
|
1685
|
-
(c) => c.trim().startsWith(`${AUTH_FLAG_COOKIE}=`)
|
|
1686
|
-
);
|
|
1687
|
-
}
|
|
1688
1495
|
var InsForgeClient = class {
|
|
1689
1496
|
constructor(config = {}) {
|
|
1690
|
-
console.log("[InsForge:Client] Initializing SDK");
|
|
1691
1497
|
this.http = new HttpClient(config);
|
|
1692
1498
|
this.tokenManager = new TokenManager(config.storage);
|
|
1693
|
-
const hasAuthCookie = hasAuthenticatedCookie();
|
|
1694
|
-
console.log("[InsForge:Client] hasAuthenticatedCookie:", hasAuthCookie);
|
|
1695
|
-
console.log("[InsForge:Client] document.cookie:", typeof document !== "undefined" ? document.cookie : "N/A (SSR)");
|
|
1696
|
-
if (hasAuthCookie) {
|
|
1697
|
-
console.log("[InsForge:Client] Switching to SecureSessionStorage");
|
|
1698
|
-
this.tokenManager.setStrategy(new SecureSessionStorage());
|
|
1699
|
-
}
|
|
1700
1499
|
if (config.edgeFunctionToken) {
|
|
1701
1500
|
this.http.setAuthToken(config.edgeFunctionToken);
|
|
1702
1501
|
this.tokenManager.saveSession({
|
|
@@ -1705,32 +1504,15 @@ var InsForgeClient = class {
|
|
|
1705
1504
|
// Will be populated by getCurrentUser()
|
|
1706
1505
|
});
|
|
1707
1506
|
}
|
|
1708
|
-
this.http.setRefreshCallback(async () => {
|
|
1709
|
-
console.log("[InsForge:Client] HTTP 401 refresh callback triggered");
|
|
1710
|
-
try {
|
|
1711
|
-
return await this.auth.refreshToken();
|
|
1712
|
-
} catch (e) {
|
|
1713
|
-
console.log("[InsForge:Client] Refresh callback failed:", e);
|
|
1714
|
-
if (this.tokenManager.getStrategyId() === "secure") {
|
|
1715
|
-
console.log("[InsForge:Client] Falling back to LocalSessionStorage");
|
|
1716
|
-
this.auth._switchToLocalStorage();
|
|
1717
|
-
}
|
|
1718
|
-
return null;
|
|
1719
|
-
}
|
|
1720
|
-
});
|
|
1721
1507
|
const existingSession = this.tokenManager.getSession();
|
|
1722
|
-
console.log("[InsForge:Client] existingSession:", !!existingSession, "strategyId:", this.tokenManager.getStrategyId());
|
|
1723
1508
|
if (existingSession?.accessToken) {
|
|
1724
1509
|
this.http.setAuthToken(existingSession.accessToken);
|
|
1725
|
-
} else if (this.tokenManager.getStrategyId() === "secure") {
|
|
1726
|
-
console.log("[InsForge:Client] Secure mode, no session in memory - will refresh on first API call");
|
|
1727
1510
|
}
|
|
1728
1511
|
this.auth = new Auth(this.http, this.tokenManager);
|
|
1729
1512
|
this.database = new Database(this.http, this.tokenManager);
|
|
1730
1513
|
this.storage = new Storage(this.http);
|
|
1731
1514
|
this.ai = new AI(this.http);
|
|
1732
1515
|
this.functions = new Functions(this.http);
|
|
1733
|
-
console.log("[InsForge:Client] SDK initialized");
|
|
1734
1516
|
}
|
|
1735
1517
|
/**
|
|
1736
1518
|
* Get the underlying HTTP client for custom requests
|
|
@@ -1744,12 +1526,6 @@ var InsForgeClient = class {
|
|
|
1744
1526
|
getHttpClient() {
|
|
1745
1527
|
return this.http;
|
|
1746
1528
|
}
|
|
1747
|
-
/**
|
|
1748
|
-
* Get the current storage strategy identifier
|
|
1749
|
-
*/
|
|
1750
|
-
getStorageStrategy() {
|
|
1751
|
-
return this.tokenManager.getStrategyId();
|
|
1752
|
-
}
|
|
1753
1529
|
/**
|
|
1754
1530
|
* Future modules will be added here:
|
|
1755
1531
|
* - database: Database operations
|
|
@@ -1773,8 +1549,6 @@ export {
|
|
|
1773
1549
|
HttpClient,
|
|
1774
1550
|
InsForgeClient,
|
|
1775
1551
|
InsForgeError,
|
|
1776
|
-
LocalSessionStorage,
|
|
1777
|
-
SecureSessionStorage,
|
|
1778
1552
|
Storage,
|
|
1779
1553
|
StorageBucket,
|
|
1780
1554
|
TokenManager,
|