@insforge/sdk 1.0.1-refresh.8 → 1.0.1-refresh.9

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.mjs CHANGED
@@ -140,6 +140,7 @@ var HttpClient = class {
140
140
  var TOKEN_KEY = "insforge-auth-token";
141
141
  var USER_KEY = "insforge-auth-user";
142
142
  var AUTH_FLAG_COOKIE = "isAuthenticated";
143
+ var CSRF_TOKEN_COOKIE = "insforge_csrf_token";
143
144
  function hasAuthCookie() {
144
145
  if (typeof document === "undefined") return false;
145
146
  return document.cookie.split(";").some(
@@ -155,6 +156,21 @@ function clearAuthCookie() {
155
156
  if (typeof document === "undefined") return;
156
157
  document.cookie = `${AUTH_FLAG_COOKIE}=; path=/; max-age=0; SameSite=Lax`;
157
158
  }
159
+ function getCsrfToken() {
160
+ if (typeof document === "undefined") return null;
161
+ const match = document.cookie.split(";").find((c) => c.trim().startsWith(`${CSRF_TOKEN_COOKIE}=`));
162
+ if (!match) return null;
163
+ return match.split("=")[1] || null;
164
+ }
165
+ function setCsrfToken(token) {
166
+ if (typeof document === "undefined") return;
167
+ const maxAge = 7 * 24 * 60 * 60;
168
+ document.cookie = `${CSRF_TOKEN_COOKIE}=${token}; path=/; max-age=${maxAge}; SameSite=Lax`;
169
+ }
170
+ function clearCsrfToken() {
171
+ if (typeof document === "undefined") return;
172
+ document.cookie = `${CSRF_TOKEN_COOKIE}=; path=/; max-age=0; SameSite=Lax`;
173
+ }
158
174
  var TokenManager = class {
159
175
  constructor(storage) {
160
176
  // In-memory storage
@@ -423,8 +439,12 @@ var Auth = class {
423
439
  }
424
440
  if (hasAuthCookie()) {
425
441
  try {
442
+ const csrfToken = getCsrfToken();
426
443
  const response = await this.http.post(
427
- "/api/auth/refresh"
444
+ "/api/auth/refresh",
445
+ {
446
+ headers: csrfToken ? { "X-CSRF-Token": csrfToken } : {}
447
+ }
428
448
  );
429
449
  if (response.accessToken) {
430
450
  this.tokenManager.setMemoryMode();
@@ -433,6 +453,9 @@ var Auth = class {
433
453
  if (response.user) {
434
454
  this.tokenManager.setUser(response.user);
435
455
  }
456
+ if (response.csrfToken) {
457
+ setCsrfToken(response.csrfToken);
458
+ }
436
459
  return { isLoggedIn: true };
437
460
  }
438
461
  } catch (error) {
@@ -448,6 +471,7 @@ var Auth = class {
448
471
  }
449
472
  if (error.statusCode === 401 || error.statusCode === 403) {
450
473
  clearAuthCookie();
474
+ clearCsrfToken();
451
475
  return { isLoggedIn: false };
452
476
  }
453
477
  }
@@ -477,6 +501,7 @@ var Auth = class {
477
501
  const userId = params.get("user_id");
478
502
  const email = params.get("email");
479
503
  const name = params.get("name");
504
+ const csrfToken = params.get("csrf_token");
480
505
  if (accessToken && userId && email) {
481
506
  const session = {
482
507
  accessToken,
@@ -494,11 +519,15 @@ var Auth = class {
494
519
  this.http.setAuthToken(accessToken);
495
520
  this.tokenManager.saveSession(session);
496
521
  setAuthCookie();
522
+ if (csrfToken) {
523
+ setCsrfToken(csrfToken);
524
+ }
497
525
  const url = new URL(window.location.href);
498
526
  url.searchParams.delete("access_token");
499
527
  url.searchParams.delete("user_id");
500
528
  url.searchParams.delete("email");
501
529
  url.searchParams.delete("name");
530
+ url.searchParams.delete("csrf_token");
502
531
  if (params.has("error")) {
503
532
  url.searchParams.delete("error");
504
533
  }
@@ -522,6 +551,9 @@ var Auth = class {
522
551
  this.tokenManager.saveSession(session);
523
552
  setAuthCookie();
524
553
  this.http.setAuthToken(response.accessToken);
554
+ if (response.csrfToken) {
555
+ setCsrfToken(response.csrfToken);
556
+ }
525
557
  }
526
558
  return {
527
559
  data: response,
@@ -555,6 +587,9 @@ var Auth = class {
555
587
  this.tokenManager.saveSession(session);
556
588
  setAuthCookie();
557
589
  this.http.setAuthToken(response.accessToken);
590
+ if (response.csrfToken) {
591
+ setCsrfToken(response.csrfToken);
592
+ }
558
593
  }
559
594
  return {
560
595
  data: response,
@@ -620,6 +655,7 @@ var Auth = class {
620
655
  this.tokenManager.clearSession();
621
656
  this.http.setAuthToken(null);
622
657
  clearAuthCookie();
658
+ clearCsrfToken();
623
659
  return { error: null };
624
660
  } catch (error) {
625
661
  return {
@@ -949,6 +985,9 @@ var Auth = class {
949
985
  this.tokenManager.saveSession(session);
950
986
  this.http.setAuthToken(response.accessToken);
951
987
  setAuthCookie();
988
+ if (response.csrfToken) {
989
+ setCsrfToken(response.csrfToken);
990
+ }
952
991
  }
953
992
  return {
954
993
  data: response,