@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.js
CHANGED
|
@@ -27,8 +27,6 @@ __export(index_exports, {
|
|
|
27
27
|
HttpClient: () => HttpClient,
|
|
28
28
|
InsForgeClient: () => InsForgeClient,
|
|
29
29
|
InsForgeError: () => InsForgeError,
|
|
30
|
-
LocalSessionStorage: () => LocalSessionStorage,
|
|
31
|
-
SecureSessionStorage: () => SecureSessionStorage,
|
|
32
30
|
Storage: () => Storage,
|
|
33
31
|
StorageBucket: () => StorageBucket,
|
|
34
32
|
TokenManager: () => TokenManager,
|
|
@@ -60,8 +58,6 @@ var InsForgeError = class _InsForgeError extends Error {
|
|
|
60
58
|
var HttpClient = class {
|
|
61
59
|
constructor(config) {
|
|
62
60
|
this.userToken = null;
|
|
63
|
-
this.isRefreshing = false;
|
|
64
|
-
this.refreshQueue = [];
|
|
65
61
|
this.baseUrl = config.baseUrl || "http://localhost:7130";
|
|
66
62
|
this.fetch = config.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
|
|
67
63
|
this.anonKey = config.anonKey;
|
|
@@ -74,12 +70,6 @@ var HttpClient = class {
|
|
|
74
70
|
);
|
|
75
71
|
}
|
|
76
72
|
}
|
|
77
|
-
/**
|
|
78
|
-
* Set the refresh callback for automatic token refresh on 401
|
|
79
|
-
*/
|
|
80
|
-
setRefreshCallback(callback) {
|
|
81
|
-
this.refreshCallback = callback;
|
|
82
|
-
}
|
|
83
73
|
buildUrl(path, params) {
|
|
84
74
|
const url = new URL(path, this.baseUrl);
|
|
85
75
|
if (params) {
|
|
@@ -96,9 +86,6 @@ var HttpClient = class {
|
|
|
96
86
|
return url.toString();
|
|
97
87
|
}
|
|
98
88
|
async request(method, path, options = {}) {
|
|
99
|
-
return this.performRequest(method, path, options, false);
|
|
100
|
-
}
|
|
101
|
-
async performRequest(method, path, options = {}, isRetry = false) {
|
|
102
89
|
const { params, headers = {}, body, ...fetchOptions } = options;
|
|
103
90
|
const url = this.buildUrl(path, params);
|
|
104
91
|
const requestHeaders = {
|
|
@@ -124,17 +111,9 @@ var HttpClient = class {
|
|
|
124
111
|
method,
|
|
125
112
|
headers: requestHeaders,
|
|
126
113
|
body: processedBody,
|
|
127
|
-
|
|
128
|
-
|
|
114
|
+
credentials: "include",
|
|
115
|
+
...fetchOptions
|
|
129
116
|
});
|
|
130
|
-
const isRefreshEndpoint = path.includes("/api/auth/refresh") || path.includes("/api/auth/logout");
|
|
131
|
-
if (response.status === 401 && !isRetry && !isRefreshEndpoint && this.refreshCallback) {
|
|
132
|
-
const newToken = await this.handleTokenRefresh();
|
|
133
|
-
if (newToken) {
|
|
134
|
-
this.setAuthToken(newToken);
|
|
135
|
-
return this.performRequest(method, path, options, true);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
117
|
if (response.status === 204) {
|
|
139
118
|
return void 0;
|
|
140
119
|
}
|
|
@@ -166,38 +145,6 @@ var HttpClient = class {
|
|
|
166
145
|
}
|
|
167
146
|
return data;
|
|
168
147
|
}
|
|
169
|
-
/**
|
|
170
|
-
* Handle token refresh with queue to prevent duplicate refreshes
|
|
171
|
-
* Multiple concurrent 401s will wait for a single refresh to complete
|
|
172
|
-
*/
|
|
173
|
-
async handleTokenRefresh() {
|
|
174
|
-
if (this.isRefreshing) {
|
|
175
|
-
return new Promise((resolve, reject) => {
|
|
176
|
-
this.refreshQueue.push({ resolve, reject });
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
this.isRefreshing = true;
|
|
180
|
-
try {
|
|
181
|
-
const newToken = await this.refreshCallback?.();
|
|
182
|
-
this.refreshQueue.forEach(({ resolve, reject }) => {
|
|
183
|
-
if (newToken) {
|
|
184
|
-
resolve(newToken);
|
|
185
|
-
} else {
|
|
186
|
-
reject(new Error("Token refresh failed"));
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
this.refreshQueue = [];
|
|
190
|
-
return newToken || null;
|
|
191
|
-
} catch (error) {
|
|
192
|
-
this.refreshQueue.forEach(({ reject }) => {
|
|
193
|
-
reject(error instanceof Error ? error : new Error("Token refresh failed"));
|
|
194
|
-
});
|
|
195
|
-
this.refreshQueue = [];
|
|
196
|
-
return null;
|
|
197
|
-
} finally {
|
|
198
|
-
this.isRefreshing = false;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
148
|
get(path, options) {
|
|
202
149
|
return this.request("GET", path, options);
|
|
203
150
|
}
|
|
@@ -226,58 +173,32 @@ var HttpClient = class {
|
|
|
226
173
|
}
|
|
227
174
|
};
|
|
228
175
|
|
|
229
|
-
// src/lib/
|
|
176
|
+
// src/lib/token-manager.ts
|
|
230
177
|
var TOKEN_KEY = "insforge-auth-token";
|
|
231
178
|
var USER_KEY = "insforge-auth-user";
|
|
232
179
|
var AUTH_FLAG_COOKIE = "isAuthenticated";
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
return this.accessToken;
|
|
252
|
-
}
|
|
253
|
-
setAccessToken(token) {
|
|
254
|
-
this.accessToken = token;
|
|
255
|
-
}
|
|
256
|
-
getUser() {
|
|
257
|
-
return this.user;
|
|
258
|
-
}
|
|
259
|
-
setUser(user) {
|
|
260
|
-
this.user = user;
|
|
261
|
-
}
|
|
262
|
-
clearSession() {
|
|
180
|
+
function hasAuthCookie() {
|
|
181
|
+
if (typeof document === "undefined") return false;
|
|
182
|
+
return document.cookie.split(";").some(
|
|
183
|
+
(c) => c.trim().startsWith(`${AUTH_FLAG_COOKIE}=`)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
function setAuthCookie() {
|
|
187
|
+
if (typeof document === "undefined") return;
|
|
188
|
+
const maxAge = 7 * 24 * 60 * 60;
|
|
189
|
+
document.cookie = `${AUTH_FLAG_COOKIE}=true; path=/; max-age=${maxAge}; SameSite=Lax`;
|
|
190
|
+
}
|
|
191
|
+
function clearAuthCookie() {
|
|
192
|
+
if (typeof document === "undefined") return;
|
|
193
|
+
document.cookie = `${AUTH_FLAG_COOKIE}=; path=/; max-age=0; SameSite=Lax`;
|
|
194
|
+
}
|
|
195
|
+
var TokenManager = class {
|
|
196
|
+
constructor(storage) {
|
|
197
|
+
// In-memory storage
|
|
263
198
|
this.accessToken = null;
|
|
264
199
|
this.user = null;
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (this.accessToken) return false;
|
|
268
|
-
return this.hasAuthFlag();
|
|
269
|
-
}
|
|
270
|
-
// --- Private: Auth Flag Cookie Detection (SDK-managed on frontend domain) ---
|
|
271
|
-
hasAuthFlag() {
|
|
272
|
-
if (typeof document === "undefined") return false;
|
|
273
|
-
return document.cookie.split(";").some(
|
|
274
|
-
(c) => c.trim().startsWith(`${AUTH_FLAG_COOKIE}=`)
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
var LocalSessionStorage = class {
|
|
279
|
-
constructor(storage) {
|
|
280
|
-
this.strategyId = "local";
|
|
200
|
+
// Mode: 'memory' (new backend) or 'storage' (legacy backend, default)
|
|
201
|
+
this._mode = "storage";
|
|
281
202
|
if (storage) {
|
|
282
203
|
this.storage = storage;
|
|
283
204
|
} else if (typeof window !== "undefined" && window.localStorage) {
|
|
@@ -295,126 +216,111 @@ var LocalSessionStorage = class {
|
|
|
295
216
|
};
|
|
296
217
|
}
|
|
297
218
|
}
|
|
298
|
-
saveSession(session) {
|
|
299
|
-
this.storage.setItem(TOKEN_KEY, session.accessToken);
|
|
300
|
-
this.storage.setItem(USER_KEY, JSON.stringify(session.user));
|
|
301
|
-
}
|
|
302
|
-
getSession() {
|
|
303
|
-
const token = this.storage.getItem(TOKEN_KEY);
|
|
304
|
-
const userStr = this.storage.getItem(USER_KEY);
|
|
305
|
-
if (!token || !userStr) return null;
|
|
306
|
-
try {
|
|
307
|
-
const user = JSON.parse(userStr);
|
|
308
|
-
return { accessToken: token, user };
|
|
309
|
-
} catch {
|
|
310
|
-
this.clearSession();
|
|
311
|
-
return null;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
getAccessToken() {
|
|
315
|
-
const token = this.storage.getItem(TOKEN_KEY);
|
|
316
|
-
return typeof token === "string" ? token : null;
|
|
317
|
-
}
|
|
318
|
-
setAccessToken(token) {
|
|
319
|
-
this.storage.setItem(TOKEN_KEY, token);
|
|
320
|
-
}
|
|
321
|
-
getUser() {
|
|
322
|
-
const userStr = this.storage.getItem(USER_KEY);
|
|
323
|
-
if (!userStr) return null;
|
|
324
|
-
try {
|
|
325
|
-
return JSON.parse(userStr);
|
|
326
|
-
} catch {
|
|
327
|
-
return null;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
setUser(user) {
|
|
331
|
-
this.storage.setItem(USER_KEY, JSON.stringify(user));
|
|
332
|
-
}
|
|
333
|
-
clearSession() {
|
|
334
|
-
this.storage.removeItem(TOKEN_KEY);
|
|
335
|
-
this.storage.removeItem(USER_KEY);
|
|
336
|
-
}
|
|
337
|
-
shouldAttemptRefresh() {
|
|
338
|
-
return false;
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
// src/lib/token-manager.ts
|
|
343
|
-
var TokenManager = class {
|
|
344
219
|
/**
|
|
345
|
-
*
|
|
346
|
-
* @param storage - Optional custom storage adapter (used for initial LocalSessionStorage)
|
|
220
|
+
* Get current mode
|
|
347
221
|
*/
|
|
348
|
-
|
|
349
|
-
this.
|
|
222
|
+
get mode() {
|
|
223
|
+
return this._mode;
|
|
350
224
|
}
|
|
351
225
|
/**
|
|
352
|
-
* Set
|
|
353
|
-
* Called after capability discovery to switch to the appropriate strategy
|
|
226
|
+
* Set mode to memory (new backend with cookies + memory)
|
|
354
227
|
*/
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
if (existingSession && previousId !== strategy.strategyId) {
|
|
360
|
-
strategy.saveSession(existingSession);
|
|
228
|
+
setMemoryMode() {
|
|
229
|
+
if (this._mode === "storage") {
|
|
230
|
+
this.storage.removeItem(TOKEN_KEY);
|
|
231
|
+
this.storage.removeItem(USER_KEY);
|
|
361
232
|
}
|
|
233
|
+
this._mode = "memory";
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Set mode to storage (legacy backend with localStorage)
|
|
237
|
+
* Also loads existing session from localStorage
|
|
238
|
+
*/
|
|
239
|
+
setStorageMode() {
|
|
240
|
+
this._mode = "storage";
|
|
241
|
+
this.loadFromStorage();
|
|
362
242
|
}
|
|
363
243
|
/**
|
|
364
|
-
*
|
|
244
|
+
* Load session from localStorage
|
|
365
245
|
*/
|
|
366
|
-
|
|
367
|
-
|
|
246
|
+
loadFromStorage() {
|
|
247
|
+
const token = this.storage.getItem(TOKEN_KEY);
|
|
248
|
+
const userStr = this.storage.getItem(USER_KEY);
|
|
249
|
+
if (token && userStr) {
|
|
250
|
+
try {
|
|
251
|
+
this.accessToken = token;
|
|
252
|
+
this.user = JSON.parse(userStr);
|
|
253
|
+
} catch {
|
|
254
|
+
this.clearSession();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
368
257
|
}
|
|
369
|
-
// --- Delegated Methods ---
|
|
370
258
|
/**
|
|
371
|
-
* Save session
|
|
259
|
+
* Save session (memory always, localStorage only in storage mode)
|
|
372
260
|
*/
|
|
373
261
|
saveSession(session) {
|
|
374
|
-
this.
|
|
262
|
+
this.accessToken = session.accessToken;
|
|
263
|
+
this.user = session.user;
|
|
264
|
+
if (this._mode === "storage") {
|
|
265
|
+
this.storage.setItem(TOKEN_KEY, session.accessToken);
|
|
266
|
+
this.storage.setItem(USER_KEY, JSON.stringify(session.user));
|
|
267
|
+
}
|
|
375
268
|
}
|
|
376
269
|
/**
|
|
377
270
|
* Get current session
|
|
378
271
|
*/
|
|
379
272
|
getSession() {
|
|
380
|
-
|
|
273
|
+
if (!this.accessToken || !this.user) return null;
|
|
274
|
+
return {
|
|
275
|
+
accessToken: this.accessToken,
|
|
276
|
+
user: this.user
|
|
277
|
+
};
|
|
381
278
|
}
|
|
382
279
|
/**
|
|
383
280
|
* Get access token
|
|
384
281
|
*/
|
|
385
282
|
getAccessToken() {
|
|
386
|
-
return this.
|
|
283
|
+
return this.accessToken;
|
|
387
284
|
}
|
|
388
285
|
/**
|
|
389
|
-
*
|
|
286
|
+
* Set access token
|
|
390
287
|
*/
|
|
391
288
|
setAccessToken(token) {
|
|
392
|
-
this.
|
|
289
|
+
this.accessToken = token;
|
|
290
|
+
if (this._mode === "storage") {
|
|
291
|
+
this.storage.setItem(TOKEN_KEY, token);
|
|
292
|
+
}
|
|
393
293
|
}
|
|
394
294
|
/**
|
|
395
|
-
* Get user
|
|
295
|
+
* Get user
|
|
396
296
|
*/
|
|
397
297
|
getUser() {
|
|
398
|
-
return this.
|
|
298
|
+
return this.user;
|
|
399
299
|
}
|
|
400
300
|
/**
|
|
401
|
-
*
|
|
301
|
+
* Set user
|
|
402
302
|
*/
|
|
403
303
|
setUser(user) {
|
|
404
|
-
this.
|
|
304
|
+
this.user = user;
|
|
305
|
+
if (this._mode === "storage") {
|
|
306
|
+
this.storage.setItem(USER_KEY, JSON.stringify(user));
|
|
307
|
+
}
|
|
405
308
|
}
|
|
406
309
|
/**
|
|
407
|
-
* Clear
|
|
310
|
+
* Clear session (both memory and localStorage)
|
|
408
311
|
*/
|
|
409
312
|
clearSession() {
|
|
410
|
-
this.
|
|
313
|
+
this.accessToken = null;
|
|
314
|
+
this.user = null;
|
|
315
|
+
this.storage.removeItem(TOKEN_KEY);
|
|
316
|
+
this.storage.removeItem(USER_KEY);
|
|
411
317
|
}
|
|
412
318
|
/**
|
|
413
|
-
* Check if
|
|
414
|
-
* (e.g., on page reload in secure mode)
|
|
319
|
+
* Check if there's a session in localStorage (for legacy detection)
|
|
415
320
|
*/
|
|
416
|
-
|
|
417
|
-
|
|
321
|
+
hasStoredSession() {
|
|
322
|
+
const token = this.storage.getItem(TOKEN_KEY);
|
|
323
|
+
return !!token;
|
|
418
324
|
}
|
|
419
325
|
};
|
|
420
326
|
|
|
@@ -531,75 +437,75 @@ var Auth = class {
|
|
|
531
437
|
this.detectAuthCallback();
|
|
532
438
|
}
|
|
533
439
|
/**
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
* Called when backend returns sessionMode: 'secure'
|
|
552
|
-
* @internal
|
|
553
|
-
*/
|
|
554
|
-
_switchToSecureStorage() {
|
|
555
|
-
console.log("[InsForge:Auth] _switchToSecureStorage() called, current strategy:", this.tokenManager.getStrategyId());
|
|
556
|
-
if (this.tokenManager.getStrategyId() === "secure") {
|
|
557
|
-
console.log("[InsForge:Auth] _switchToSecureStorage() - already in secure mode, skipping");
|
|
558
|
-
return;
|
|
440
|
+
* Restore session on app initialization
|
|
441
|
+
*
|
|
442
|
+
* @returns Object with isLoggedIn status
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* ```typescript
|
|
446
|
+
* const client = new InsForgeClient({ baseUrl: '...' });
|
|
447
|
+
* const { isLoggedIn } = await client.auth.restoreSession();
|
|
448
|
+
*
|
|
449
|
+
* if (isLoggedIn) {
|
|
450
|
+
* const { data } = await client.auth.getCurrentUser();
|
|
451
|
+
* }
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
454
|
+
async restoreSession() {
|
|
455
|
+
if (typeof window === "undefined") {
|
|
456
|
+
return { isLoggedIn: false };
|
|
559
457
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (typeof localStorage !== "undefined") {
|
|
563
|
-
console.log("[InsForge:Auth] _switchToSecureStorage() - clearing localStorage");
|
|
564
|
-
localStorage.removeItem(TOKEN_KEY);
|
|
565
|
-
localStorage.removeItem(USER_KEY);
|
|
458
|
+
if (this.tokenManager.getAccessToken()) {
|
|
459
|
+
return { isLoggedIn: true };
|
|
566
460
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
461
|
+
if (hasAuthCookie()) {
|
|
462
|
+
try {
|
|
463
|
+
const response = await this.http.post(
|
|
464
|
+
"/api/auth/refresh"
|
|
465
|
+
);
|
|
466
|
+
if (response.accessToken) {
|
|
467
|
+
this.tokenManager.setMemoryMode();
|
|
468
|
+
this.tokenManager.setAccessToken(response.accessToken);
|
|
469
|
+
this.http.setAuthToken(response.accessToken);
|
|
470
|
+
if (response.user) {
|
|
471
|
+
this.tokenManager.setUser(response.user);
|
|
472
|
+
}
|
|
473
|
+
return { isLoggedIn: true };
|
|
474
|
+
}
|
|
475
|
+
} catch (error) {
|
|
476
|
+
if (error instanceof InsForgeError) {
|
|
477
|
+
if (error.statusCode === 404) {
|
|
478
|
+
this.tokenManager.setStorageMode();
|
|
479
|
+
const token = this.tokenManager.getAccessToken();
|
|
480
|
+
if (token) {
|
|
481
|
+
this.http.setAuthToken(token);
|
|
482
|
+
return { isLoggedIn: true };
|
|
483
|
+
}
|
|
484
|
+
return { isLoggedIn: false };
|
|
485
|
+
}
|
|
486
|
+
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
487
|
+
clearAuthCookie();
|
|
488
|
+
return { isLoggedIn: false };
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return { isLoggedIn: false };
|
|
492
|
+
}
|
|
571
493
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (this.tokenManager.getStrategyId() === "local") return;
|
|
580
|
-
const currentSession = this.tokenManager.getSession();
|
|
581
|
-
this.tokenManager.setStrategy(new LocalSessionStorage());
|
|
582
|
-
this.clearAuthenticatedCookie();
|
|
583
|
-
if (currentSession) {
|
|
584
|
-
this.tokenManager.saveSession(currentSession);
|
|
494
|
+
if (this.tokenManager.hasStoredSession()) {
|
|
495
|
+
this.tokenManager.setStorageMode();
|
|
496
|
+
const token = this.tokenManager.getAccessToken();
|
|
497
|
+
if (token) {
|
|
498
|
+
this.http.setAuthToken(token);
|
|
499
|
+
return { isLoggedIn: true };
|
|
500
|
+
}
|
|
585
501
|
}
|
|
502
|
+
return { isLoggedIn: false };
|
|
586
503
|
}
|
|
587
504
|
/**
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
505
|
+
* Automatically detect and handle OAuth callback parameters in the URL
|
|
506
|
+
* This runs on initialization to seamlessly complete the OAuth flow
|
|
507
|
+
* Matches the backend's OAuth callback response (backend/src/api/routes/auth.ts:540-544)
|
|
591
508
|
*/
|
|
592
|
-
_detectStorageFromResponse(sessionMode) {
|
|
593
|
-
console.log("[InsForge:Auth] _detectStorageFromResponse() - sessionMode:", sessionMode);
|
|
594
|
-
if (sessionMode === "secure") {
|
|
595
|
-
this._switchToSecureStorage();
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* Automatically detect and handle OAuth callback parameters in the URL
|
|
600
|
-
* This runs on initialization to seamlessly complete the OAuth flow
|
|
601
|
-
* Matches the backend's OAuth callback response (backend/src/api/routes/auth.ts:540-544)
|
|
602
|
-
*/
|
|
603
509
|
detectAuthCallback() {
|
|
604
510
|
if (typeof window === "undefined") return;
|
|
605
511
|
try {
|
|
@@ -608,9 +514,7 @@ var Auth = class {
|
|
|
608
514
|
const userId = params.get("user_id");
|
|
609
515
|
const email = params.get("email");
|
|
610
516
|
const name = params.get("name");
|
|
611
|
-
const sessionMode = params.get("session_mode");
|
|
612
517
|
if (accessToken && userId && email) {
|
|
613
|
-
this._detectStorageFromResponse(sessionMode || void 0);
|
|
614
518
|
const session = {
|
|
615
519
|
accessToken,
|
|
616
520
|
user: {
|
|
@@ -624,14 +528,14 @@ var Auth = class {
|
|
|
624
528
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
625
529
|
}
|
|
626
530
|
};
|
|
627
|
-
this.tokenManager.saveSession(session);
|
|
628
531
|
this.http.setAuthToken(accessToken);
|
|
532
|
+
this.tokenManager.saveSession(session);
|
|
533
|
+
setAuthCookie();
|
|
629
534
|
const url = new URL(window.location.href);
|
|
630
535
|
url.searchParams.delete("access_token");
|
|
631
536
|
url.searchParams.delete("user_id");
|
|
632
537
|
url.searchParams.delete("email");
|
|
633
538
|
url.searchParams.delete("name");
|
|
634
|
-
url.searchParams.delete("session_mode");
|
|
635
539
|
if (params.has("error")) {
|
|
636
540
|
url.searchParams.delete("error");
|
|
637
541
|
}
|
|
@@ -647,16 +551,13 @@ var Auth = class {
|
|
|
647
551
|
async signUp(request) {
|
|
648
552
|
try {
|
|
649
553
|
const response = await this.http.post("/api/auth/users", request);
|
|
650
|
-
|
|
651
|
-
this._detectStorageFromResponse(sessionMode);
|
|
652
|
-
if (response.accessToken && response.user) {
|
|
554
|
+
if (response.accessToken && response.user && !isHostedAuthEnvironment()) {
|
|
653
555
|
const session = {
|
|
654
556
|
accessToken: response.accessToken,
|
|
655
557
|
user: response.user
|
|
656
558
|
};
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
}
|
|
559
|
+
this.tokenManager.saveSession(session);
|
|
560
|
+
setAuthCookie();
|
|
660
561
|
this.http.setAuthToken(response.accessToken);
|
|
661
562
|
}
|
|
662
563
|
return {
|
|
@@ -683,23 +584,15 @@ var Auth = class {
|
|
|
683
584
|
async signInWithPassword(request) {
|
|
684
585
|
try {
|
|
685
586
|
const response = await this.http.post("/api/auth/sessions", request);
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
id: "",
|
|
692
|
-
email: "",
|
|
693
|
-
name: "",
|
|
694
|
-
emailVerified: false,
|
|
695
|
-
createdAt: "",
|
|
696
|
-
updatedAt: ""
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
if (!isHostedAuthEnvironment()) {
|
|
587
|
+
if (response.accessToken && response.user && !isHostedAuthEnvironment()) {
|
|
588
|
+
const session = {
|
|
589
|
+
accessToken: response.accessToken,
|
|
590
|
+
user: response.user
|
|
591
|
+
};
|
|
700
592
|
this.tokenManager.saveSession(session);
|
|
593
|
+
setAuthCookie();
|
|
594
|
+
this.http.setAuthToken(response.accessToken);
|
|
701
595
|
}
|
|
702
|
-
this.http.setAuthToken(response.accessToken || "");
|
|
703
596
|
return {
|
|
704
597
|
data: response,
|
|
705
598
|
error: null
|
|
@@ -754,28 +647,18 @@ var Auth = class {
|
|
|
754
647
|
}
|
|
755
648
|
/**
|
|
756
649
|
* Sign out the current user
|
|
757
|
-
* In modern mode, also calls backend to clear the refresh token cookie
|
|
758
650
|
*/
|
|
759
651
|
async signOut() {
|
|
760
|
-
console.log("[InsForge:Auth] signOut() called");
|
|
761
|
-
console.log("[InsForge:Auth] signOut() stack trace:", new Error().stack);
|
|
762
652
|
try {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
await this.http.post("/api/auth/logout");
|
|
767
|
-
console.log("[InsForge:Auth] signOut() - backend logout successful");
|
|
768
|
-
} catch (e) {
|
|
769
|
-
console.log("[InsForge:Auth] signOut() - backend logout failed (ignored):", e);
|
|
770
|
-
}
|
|
653
|
+
try {
|
|
654
|
+
await this.http.post("/api/auth/logout");
|
|
655
|
+
} catch {
|
|
771
656
|
}
|
|
772
657
|
this.tokenManager.clearSession();
|
|
773
658
|
this.http.setAuthToken(null);
|
|
774
|
-
|
|
775
|
-
console.log("[InsForge:Auth] signOut() - completed");
|
|
659
|
+
clearAuthCookie();
|
|
776
660
|
return { error: null };
|
|
777
661
|
} catch (error) {
|
|
778
|
-
console.error("[InsForge:Auth] signOut() - error:", error);
|
|
779
662
|
return {
|
|
780
663
|
error: new InsForgeError(
|
|
781
664
|
"Failed to sign out",
|
|
@@ -785,52 +668,6 @@ var Auth = class {
|
|
|
785
668
|
};
|
|
786
669
|
}
|
|
787
670
|
}
|
|
788
|
-
/**
|
|
789
|
-
* Refresh the access token using the httpOnly refresh token cookie
|
|
790
|
-
* Only works when backend supports secure session storage (httpOnly cookies)
|
|
791
|
-
*
|
|
792
|
-
* @returns New access token or throws an error
|
|
793
|
-
*/
|
|
794
|
-
async refreshToken() {
|
|
795
|
-
console.log("[InsForge:Auth] refreshToken() called");
|
|
796
|
-
try {
|
|
797
|
-
const response = await this.http.post(
|
|
798
|
-
"/api/auth/refresh"
|
|
799
|
-
);
|
|
800
|
-
console.log("[InsForge:Auth] refreshToken() - response received, hasAccessToken:", !!response.accessToken);
|
|
801
|
-
if (response.accessToken) {
|
|
802
|
-
this._detectStorageFromResponse(response.sessionMode);
|
|
803
|
-
this.tokenManager.setAccessToken(response.accessToken);
|
|
804
|
-
this.http.setAuthToken(response.accessToken);
|
|
805
|
-
if (response.user) {
|
|
806
|
-
this.tokenManager.setUser(response.user);
|
|
807
|
-
}
|
|
808
|
-
console.log("[InsForge:Auth] refreshToken() - success");
|
|
809
|
-
return response.accessToken;
|
|
810
|
-
}
|
|
811
|
-
throw new InsForgeError(
|
|
812
|
-
"No access token in refresh response",
|
|
813
|
-
500,
|
|
814
|
-
"REFRESH_FAILED"
|
|
815
|
-
);
|
|
816
|
-
} catch (error) {
|
|
817
|
-
console.error("[InsForge:Auth] refreshToken() - error:", error);
|
|
818
|
-
if (error instanceof InsForgeError) {
|
|
819
|
-
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
820
|
-
console.log("[InsForge:Auth] refreshToken() - clearing session due to 401/403");
|
|
821
|
-
this.tokenManager.clearSession();
|
|
822
|
-
this.http.setAuthToken(null);
|
|
823
|
-
this.clearAuthenticatedCookie();
|
|
824
|
-
}
|
|
825
|
-
throw error;
|
|
826
|
-
}
|
|
827
|
-
throw new InsForgeError(
|
|
828
|
-
"Token refresh failed",
|
|
829
|
-
500,
|
|
830
|
-
"REFRESH_FAILED"
|
|
831
|
-
);
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
671
|
/**
|
|
835
672
|
* Get all public authentication configuration (OAuth + Email)
|
|
836
673
|
* Returns both OAuth providers and email authentication settings in one request
|
|
@@ -871,40 +708,19 @@ var Auth = class {
|
|
|
871
708
|
/**
|
|
872
709
|
* Get the current user with full profile information
|
|
873
710
|
* Returns both auth info (id, email, role) and profile data (dynamic fields from users table)
|
|
874
|
-
*
|
|
875
|
-
* In secure session mode (httpOnly cookie), this method will automatically attempt
|
|
876
|
-
* to refresh the session if no access token is available (e.g., after page reload).
|
|
877
711
|
*/
|
|
878
712
|
async getCurrentUser() {
|
|
879
|
-
console.log("[InsForge:Auth] getCurrentUser() called");
|
|
880
713
|
try {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
console.log("[InsForge:Auth] getCurrentUser() - hasAccessToken:", !!accessToken, "shouldAttemptRefresh:", shouldRefresh);
|
|
884
|
-
if (!accessToken && shouldRefresh) {
|
|
885
|
-
console.log("[InsForge:Auth] getCurrentUser() - attempting refresh");
|
|
886
|
-
try {
|
|
887
|
-
accessToken = await this.refreshToken();
|
|
888
|
-
} catch (error) {
|
|
889
|
-
console.log("[InsForge:Auth] getCurrentUser() - refresh failed:", error);
|
|
890
|
-
if (error instanceof InsForgeError && (error.statusCode === 401 || error.statusCode === 403)) {
|
|
891
|
-
return { data: null, error };
|
|
892
|
-
}
|
|
893
|
-
return { data: null, error: error instanceof InsForgeError ? error : new InsForgeError("Token refresh failed", 500, "REFRESH_FAILED") };
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
if (!accessToken) {
|
|
897
|
-
console.log("[InsForge:Auth] getCurrentUser() - no access token, returning null");
|
|
714
|
+
const session = this.tokenManager.getSession();
|
|
715
|
+
if (!session?.accessToken) {
|
|
898
716
|
return { data: null, error: null };
|
|
899
717
|
}
|
|
900
|
-
this.http.setAuthToken(accessToken);
|
|
901
|
-
console.log("[InsForge:Auth] getCurrentUser() - fetching user from API");
|
|
718
|
+
this.http.setAuthToken(session.accessToken);
|
|
902
719
|
const authResponse = await this.http.get("/api/auth/sessions/current");
|
|
903
720
|
const { data: profile, error: profileError } = await this.database.from("users").select("*").eq("id", authResponse.user.id).single();
|
|
904
721
|
if (profileError && profileError.code !== "PGRST116") {
|
|
905
722
|
return { data: null, error: profileError };
|
|
906
723
|
}
|
|
907
|
-
console.log("[InsForge:Auth] getCurrentUser() - success");
|
|
908
724
|
return {
|
|
909
725
|
data: {
|
|
910
726
|
user: authResponse.user,
|
|
@@ -913,12 +729,8 @@ var Auth = class {
|
|
|
913
729
|
error: null
|
|
914
730
|
};
|
|
915
731
|
} catch (error) {
|
|
916
|
-
console.error("[InsForge:Auth] getCurrentUser() - catch error:", error);
|
|
917
732
|
if (error instanceof InsForgeError && error.statusCode === 401) {
|
|
918
|
-
|
|
919
|
-
this.tokenManager.clearSession();
|
|
920
|
-
this.http.setAuthToken(null);
|
|
921
|
-
this.clearAuthenticatedCookie();
|
|
733
|
+
await this.signOut();
|
|
922
734
|
return { data: null, error: null };
|
|
923
735
|
}
|
|
924
736
|
if (error instanceof InsForgeError) {
|
|
@@ -1166,15 +978,14 @@ var Auth = class {
|
|
|
1166
978
|
"/api/auth/email/verify",
|
|
1167
979
|
request
|
|
1168
980
|
);
|
|
1169
|
-
|
|
1170
|
-
this._detectStorageFromResponse(sessionMode);
|
|
1171
|
-
if (response.accessToken) {
|
|
981
|
+
if (response.accessToken && !isHostedAuthEnvironment()) {
|
|
1172
982
|
const session = {
|
|
1173
983
|
accessToken: response.accessToken,
|
|
1174
984
|
user: response.user || {}
|
|
1175
985
|
};
|
|
1176
986
|
this.tokenManager.saveSession(session);
|
|
1177
987
|
this.http.setAuthToken(response.accessToken);
|
|
988
|
+
setAuthCookie();
|
|
1178
989
|
}
|
|
1179
990
|
return {
|
|
1180
991
|
data: response,
|
|
@@ -1718,24 +1529,10 @@ var Functions = class {
|
|
|
1718
1529
|
};
|
|
1719
1530
|
|
|
1720
1531
|
// src/client.ts
|
|
1721
|
-
function hasAuthenticatedCookie() {
|
|
1722
|
-
if (typeof document === "undefined") return false;
|
|
1723
|
-
return document.cookie.split(";").some(
|
|
1724
|
-
(c) => c.trim().startsWith(`${AUTH_FLAG_COOKIE}=`)
|
|
1725
|
-
);
|
|
1726
|
-
}
|
|
1727
1532
|
var InsForgeClient = class {
|
|
1728
1533
|
constructor(config = {}) {
|
|
1729
|
-
console.log("[InsForge:Client] Initializing SDK");
|
|
1730
1534
|
this.http = new HttpClient(config);
|
|
1731
1535
|
this.tokenManager = new TokenManager(config.storage);
|
|
1732
|
-
const hasAuthCookie = hasAuthenticatedCookie();
|
|
1733
|
-
console.log("[InsForge:Client] hasAuthenticatedCookie:", hasAuthCookie);
|
|
1734
|
-
console.log("[InsForge:Client] document.cookie:", typeof document !== "undefined" ? document.cookie : "N/A (SSR)");
|
|
1735
|
-
if (hasAuthCookie) {
|
|
1736
|
-
console.log("[InsForge:Client] Switching to SecureSessionStorage");
|
|
1737
|
-
this.tokenManager.setStrategy(new SecureSessionStorage());
|
|
1738
|
-
}
|
|
1739
1536
|
if (config.edgeFunctionToken) {
|
|
1740
1537
|
this.http.setAuthToken(config.edgeFunctionToken);
|
|
1741
1538
|
this.tokenManager.saveSession({
|
|
@@ -1744,32 +1541,15 @@ var InsForgeClient = class {
|
|
|
1744
1541
|
// Will be populated by getCurrentUser()
|
|
1745
1542
|
});
|
|
1746
1543
|
}
|
|
1747
|
-
this.http.setRefreshCallback(async () => {
|
|
1748
|
-
console.log("[InsForge:Client] HTTP 401 refresh callback triggered");
|
|
1749
|
-
try {
|
|
1750
|
-
return await this.auth.refreshToken();
|
|
1751
|
-
} catch (e) {
|
|
1752
|
-
console.log("[InsForge:Client] Refresh callback failed:", e);
|
|
1753
|
-
if (this.tokenManager.getStrategyId() === "secure") {
|
|
1754
|
-
console.log("[InsForge:Client] Falling back to LocalSessionStorage");
|
|
1755
|
-
this.auth._switchToLocalStorage();
|
|
1756
|
-
}
|
|
1757
|
-
return null;
|
|
1758
|
-
}
|
|
1759
|
-
});
|
|
1760
1544
|
const existingSession = this.tokenManager.getSession();
|
|
1761
|
-
console.log("[InsForge:Client] existingSession:", !!existingSession, "strategyId:", this.tokenManager.getStrategyId());
|
|
1762
1545
|
if (existingSession?.accessToken) {
|
|
1763
1546
|
this.http.setAuthToken(existingSession.accessToken);
|
|
1764
|
-
} else if (this.tokenManager.getStrategyId() === "secure") {
|
|
1765
|
-
console.log("[InsForge:Client] Secure mode, no session in memory - will refresh on first API call");
|
|
1766
1547
|
}
|
|
1767
1548
|
this.auth = new Auth(this.http, this.tokenManager);
|
|
1768
1549
|
this.database = new Database(this.http, this.tokenManager);
|
|
1769
1550
|
this.storage = new Storage(this.http);
|
|
1770
1551
|
this.ai = new AI(this.http);
|
|
1771
1552
|
this.functions = new Functions(this.http);
|
|
1772
|
-
console.log("[InsForge:Client] SDK initialized");
|
|
1773
1553
|
}
|
|
1774
1554
|
/**
|
|
1775
1555
|
* Get the underlying HTTP client for custom requests
|
|
@@ -1783,12 +1563,6 @@ var InsForgeClient = class {
|
|
|
1783
1563
|
getHttpClient() {
|
|
1784
1564
|
return this.http;
|
|
1785
1565
|
}
|
|
1786
|
-
/**
|
|
1787
|
-
* Get the current storage strategy identifier
|
|
1788
|
-
*/
|
|
1789
|
-
getStorageStrategy() {
|
|
1790
|
-
return this.tokenManager.getStrategyId();
|
|
1791
|
-
}
|
|
1792
1566
|
/**
|
|
1793
1567
|
* Future modules will be added here:
|
|
1794
1568
|
* - database: Database operations
|
|
@@ -1813,8 +1587,6 @@ var index_default = InsForgeClient;
|
|
|
1813
1587
|
HttpClient,
|
|
1814
1588
|
InsForgeClient,
|
|
1815
1589
|
InsForgeError,
|
|
1816
|
-
LocalSessionStorage,
|
|
1817
|
-
SecureSessionStorage,
|
|
1818
1590
|
Storage,
|
|
1819
1591
|
StorageBucket,
|
|
1820
1592
|
TokenManager,
|