@fluid-app/portal-sdk 0.1.1353 → 0.1.1354

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.cjs CHANGED
@@ -2485,8 +2485,9 @@ async function deviceToken(native) {
2485
2485
  * Provides a complete logout function that:
2486
2486
  * 1. Gives up this device's push registration
2487
2487
  * 2. Clears all client-side state (query cache, IndexedDB, sessionStorage)
2488
- * 3. Submits a CSRF-checked DELETE /logout (GET /logout, which only clears the
2489
- * tenant session, when the page has no csrf-token meta tag)
2488
+ * 3. Submits a CSRF-checked DELETE /logout; without a csrf-token meta tag it
2489
+ * first reads the token from the GET /logout confirmation page, and
2490
+ * navigates there only if that fails
2490
2491
  */
2491
2492
  function submitDeleteLogout(csrfToken) {
2492
2493
  const form = document.createElement("form");
@@ -2503,6 +2504,26 @@ function submitDeleteLogout(csrfToken) {
2503
2504
  document.body.appendChild(form);
2504
2505
  form.submit();
2505
2506
  }
2507
+ async function fetchLogoutToken() {
2508
+ try {
2509
+ const response = await fetch("/logout", {
2510
+ credentials: "include",
2511
+ headers: { Accept: "text/html" }
2512
+ });
2513
+ if (!response.ok) return null;
2514
+ const doc = new DOMParser().parseFromString(await response.text(), "text/html");
2515
+ const metaToken = doc.querySelector("meta[name=\"csrf-token\"]")?.getAttribute("content");
2516
+ if (metaToken) return metaToken;
2517
+ for (const form of Array.from(doc.querySelectorAll("form"))) {
2518
+ if (!(form.getAttribute("action") ?? "").endsWith("/logout")) continue;
2519
+ const input = form.querySelector("input[name=\"authenticity_token\"]");
2520
+ if (input?.value) return input.value;
2521
+ }
2522
+ return null;
2523
+ } catch {
2524
+ return null;
2525
+ }
2526
+ }
2506
2527
  /**
2507
2528
  * Hook that returns a function clearing all client-side state: the query
2508
2529
  * cache, the IndexedDB persisted cache, sessionStorage and the FairShare SDK
@@ -2553,7 +2574,7 @@ function useLogout({ redirectUrl, onLogout } = {}) {
2553
2574
  if (redirectUrl) window.location.href = redirectUrl;
2554
2575
  else if (onLogout) onLogout();
2555
2576
  else {
2556
- const csrfToken = document.querySelector("meta[name=\"csrf-token\"]")?.getAttribute("content");
2577
+ const csrfToken = document.querySelector("meta[name=\"csrf-token\"]")?.getAttribute("content") || await fetchLogoutToken();
2557
2578
  if (csrfToken) submitDeleteLogout(csrfToken);
2558
2579
  else window.location.href = "/logout";
2559
2580
  }