@commercengine/storefront-sdk 0.12.2 → 0.12.4

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
@@ -1,5 +1,4 @@
1
1
  import createClient from "openapi-fetch";
2
- import { decodeJwt } from "jose";
3
2
 
4
3
  //#region ../sdk-core/dist/index.mjs
5
4
  /**
@@ -415,6 +414,30 @@ function getPathnameFromUrl(url) {
415
414
  //#endregion
416
415
  //#region src/lib/jwt-utils.ts
417
416
  /**
417
+ * Decode a JWT token payload without signature verification.
418
+ * This is a lightweight replacement for jose's decodeJwt.
419
+ *
420
+ * @param token - The JWT token to decode
421
+ * @returns The decoded payload
422
+ * @throws Error if the token is malformed
423
+ */
424
+ function decodeJwt(token) {
425
+ if (typeof token !== "string") throw new Error("Invalid token: must be a string");
426
+ const parts = token.split(".");
427
+ if (parts.length !== 3) throw new Error("Invalid token: must have 3 parts");
428
+ const base64Url = parts[1];
429
+ if (!base64Url) throw new Error("Invalid token: missing payload");
430
+ let base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
431
+ const padding = base64.length % 4;
432
+ if (padding) base64 += "=".repeat(4 - padding);
433
+ const binaryStr = atob(base64);
434
+ const bytes = new Uint8Array(binaryStr.length);
435
+ for (let i = 0; i < binaryStr.length; i++) bytes[i] = binaryStr.charCodeAt(i);
436
+ const payload = JSON.parse(new TextDecoder().decode(bytes));
437
+ if (typeof payload !== "object" || payload === null) throw new Error("Invalid token: payload must be an object");
438
+ return payload;
439
+ }
440
+ /**
418
441
  * Decode and extract user information from a JWT token
419
442
  *
420
443
  * @param token - The JWT token to decode
@@ -432,7 +455,7 @@ function extractUserInfoFromToken(token) {
432
455
  lastName: payload.last_name,
433
456
  storeId: payload.store_id,
434
457
  isLoggedIn: payload.is_logged_in,
435
- isAnonymous: !payload.is_logged_in,
458
+ isAnonymous: payload.is_anonymous,
436
459
  customerId: payload.customer_id,
437
460
  customerGroupId: payload.customer_group_id,
438
461
  anonymousId: payload.anonymous_id,
@@ -487,7 +510,7 @@ function isUserLoggedIn(token) {
487
510
  * @returns True if user is anonymous, false otherwise
488
511
  */
489
512
  function isUserAnonymous(token) {
490
- return extractUserInfoFromToken(token)?.isAnonymous || true;
513
+ return extractUserInfoFromToken(token)?.isAnonymous ?? true;
491
514
  }
492
515
 
493
516
  //#endregion
@@ -507,15 +530,10 @@ function isTokenReturningEndpoint(pathname) {
507
530
  "/auth/register/phone",
508
531
  "/auth/register/email",
509
532
  "/auth/verify-otp",
510
- "/auth/refresh-token"
533
+ "/auth/refresh-token",
534
+ "/auth/logout"
511
535
  ].some((endpoint) => pathname.endsWith(endpoint));
512
536
  }
513
- /**
514
- * Check if a URL path is the logout endpoint
515
- */
516
- function isLogoutEndpoint(pathname) {
517
- return pathname.endsWith("/auth/logout");
518
- }
519
537
 
520
538
  //#endregion
521
539
  //#region src/lib/middleware.ts
@@ -765,10 +783,6 @@ function createAuthMiddleware(config) {
765
783
  } catch (error) {
766
784
  console.warn("Failed to extract tokens from response:", error);
767
785
  }
768
- else if (isLogoutEndpoint(pathname)) {
769
- await config.tokenStorage.clearTokens();
770
- config.onTokensCleared?.();
771
- }
772
786
  }
773
787
  if (response.status === 401 && !isAnonymousAuthEndpoint(pathname)) {
774
788
  const currentToken = await config.tokenStorage.getAccessToken();