sanity 6.6.0-next.35 → 6.6.0-next.37

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.
@@ -629,6 +629,26 @@ interface AuthStoreOptions extends AuthConfig {
629
629
  * @internal
630
630
  */
631
631
  consumeHashToken: () => string | undefined;
632
+ /**
633
+ * Observes the session token issued by the embedding workbench "OS", if the
634
+ * Studio is running as a federated remote inside it. While it yields a token,
635
+ * that token is authoritative: it takes precedence over `loginMethod` and
636
+ * every other mechanism, is used in-memory only (never persisted), and the
637
+ * auth state tracks it over time — so an OS sign-out (`null`) transitions the
638
+ * Studio to unauthenticated. Returns `undefined` outside the workbench, in
639
+ * which case the normal auth flow runs. Defaults to a no-op.
640
+ * @internal
641
+ */
642
+ observeWorkbenchToken?: () => Observable<string | null> | undefined;
643
+ /**
644
+ * Asks the workbench "OS" to reissue its session token, called when the
645
+ * current one is rejected (a 401 surfaced as forced logout). In the workbench
646
+ * this replaces tearing down the session locally, since the OS owns it — the
647
+ * reissued token flows back through `observeWorkbenchToken`. Defaults to a
648
+ * no-op.
649
+ * @internal
650
+ */
651
+ refreshWorkbenchToken?: () => void;
632
652
  }
633
653
  /**
634
654
  * Lets the auth store's `/users/me` probe diagnose and report the failures the
@@ -662,16 +682,19 @@ declare function _createAuthStore({
662
682
  loginMethod,
663
683
  getSessionId,
664
684
  consumeHashToken,
685
+ observeWorkbenchToken,
686
+ refreshWorkbenchToken,
665
687
  getRequestErrorHandler,
666
688
  getRequestFailureDiagnostics,
667
689
  ...providerOptions
668
690
  }: AuthStoreOptions): AuthStore;
669
691
  /**
670
- * Public options for `createAuthStore`. The `getSessionId` and `consumeHashToken`
671
- * dependencies are wired automatically using the default implementations.
692
+ * Public options for `createAuthStore`. The `getSessionId`, `consumeHashToken`,
693
+ * `observeWorkbenchToken` and `refreshWorkbenchToken` dependencies are wired
694
+ * automatically using the default implementations.
672
695
  * @internal
673
696
  */
674
- type CreateAuthStoreOptions = Omit<AuthStoreOptions, 'getSessionId' | 'consumeHashToken'>;
697
+ type CreateAuthStoreOptions = Omit<AuthStoreOptions, 'getSessionId' | 'consumeHashToken' | 'observeWorkbenchToken' | 'refreshWorkbenchToken'>;
675
698
  /**
676
699
  * @internal
677
700
  */
@@ -5604,6 +5604,26 @@ function isAuthStore(maybeStore) {
5604
5604
  function isCookielessCompatibleLoginMethod(loginMethod) {
5605
5605
  return ["dual", "token"].includes(loginMethod);
5606
5606
  }
5607
+ const OS_BUS_KEY = /* @__PURE__ */ Symbol.for("sanity.os.bus");
5608
+ function isWorkbenchEnvironment() {
5609
+ return typeof globalThis == "object" && OS_BUS_KEY in globalThis;
5610
+ }
5611
+ function observeWorkbenchToken() {
5612
+ if (isWorkbenchEnvironment())
5613
+ return from(import("@sanity/workbench")).pipe(
5614
+ switchMap(({
5615
+ os
5616
+ }) => os.subscribe("auth.token")),
5617
+ // Any failure (importing the host bundle, or the subscription) means "no OS token".
5618
+ catchError(() => of(null))
5619
+ );
5620
+ }
5621
+ function refreshWorkbenchToken() {
5622
+ isWorkbenchEnvironment() && import("@sanity/workbench").then(({
5623
+ os
5624
+ }) => os.emit("auth.token.refresh", void 0), () => {
5625
+ });
5626
+ }
5607
5627
  const getCurrentUser = async (client, tag, getRequestErrorHandler, diagnostics) => {
5608
5628
  const probeClient = typeof client.withConfig == "function" ? client.withConfig({
5609
5629
  _requestHandler: void 0
@@ -5671,6 +5691,10 @@ function _createAuthStore({
5671
5691
  loginMethod = "dual",
5672
5692
  getSessionId: getSessionId2,
5673
5693
  consumeHashToken: consumeHashToken2,
5694
+ observeWorkbenchToken: observeWorkbenchToken2 = () => {
5695
+ },
5696
+ refreshWorkbenchToken: refreshWorkbenchToken2 = () => {
5697
+ },
5674
5698
  getRequestErrorHandler,
5675
5699
  getRequestFailureDiagnostics,
5676
5700
  ...providerOptions
@@ -5757,7 +5781,7 @@ function _createAuthStore({
5757
5781
  refCount: !0
5758
5782
  })), cookieAuthChanged$ = cookieAuthState.value.pipe(filter((v) => v?.authenticated !== "pending"), distinctUntilChanged(isEqual$1)), cookieWorkspaceClient$ = cookieAuthChanged$.pipe(map(({
5759
5783
  authenticated
5760
- }) => authenticated ? cookieClient : void 0)), workspaceClient$ = (loginMethod === "dual" ? dualClient$ : loginMethod === "cookie" ? cookieWorkspaceClient$ : tokenClient$).pipe(distinctUntilChanged()), dualCookieRecheck$ = loginMethod === "dual" ? cookieAuthChanged$.pipe(skip$1(1), switchMap(() => dualClient$)) : EMPTY$5, initialToken = currentTokenState?.token ?? null, dualTokenRecheck$ = loginMethod === "dual" ? tokenStorage.value.pipe(map((state) => state?.token ?? null), distinctUntilChanged(), filter((token) => typeof token == "string" && token !== initialToken), switchMap(() => dualClient$)) : EMPTY$5, authState$ = concat(initial$, merge(workspaceClient$.pipe(skip$1(1)), dualCookieRecheck$, dualTokenRecheck$).pipe(mergeMap(async (client) => {
5784
+ }) => authenticated ? cookieClient : void 0)), workspaceClient$ = (loginMethod === "dual" ? dualClient$ : loginMethod === "cookie" ? cookieWorkspaceClient$ : tokenClient$).pipe(distinctUntilChanged()), dualCookieRecheck$ = loginMethod === "dual" ? cookieAuthChanged$.pipe(skip$1(1), switchMap(() => dualClient$)) : EMPTY$5, initialToken = currentTokenState?.token ?? null, dualTokenRecheck$ = loginMethod === "dual" ? tokenStorage.value.pipe(map((state) => state?.token ?? null), distinctUntilChanged(), filter((token) => typeof token == "string" && token !== initialToken), switchMap(() => dualClient$)) : EMPTY$5, existingAuthState$ = concat(initial$, merge(workspaceClient$.pipe(skip$1(1)), dualCookieRecheck$, dualTokenRecheck$).pipe(mergeMap(async (client) => {
5761
5785
  if (!client)
5762
5786
  return {
5763
5787
  client: cookieClient,
@@ -5770,7 +5794,30 @@ function _createAuthStore({
5770
5794
  authenticated: !!currentUser?.id,
5771
5795
  currentUser: currentUser || null
5772
5796
  };
5773
- }))).pipe(
5797
+ }))), workbenchToken$ = observeWorkbenchToken2()?.pipe(shareReplay({
5798
+ bufferSize: 1,
5799
+ refCount: !0
5800
+ })), authState$ = (workbenchToken$ ? workbenchToken$.pipe(switchMap((workbenchToken) => {
5801
+ if (!workbenchToken)
5802
+ return of({
5803
+ client: cookieClient,
5804
+ authenticated: !1,
5805
+ currentUser: null
5806
+ });
5807
+ const client = clientFactory({
5808
+ ...AUTH_CLIENT_OPTIONS,
5809
+ ...hostOptions,
5810
+ projectId,
5811
+ dataset,
5812
+ token: workbenchToken,
5813
+ ignoreBrowserTokenWarning: !0
5814
+ });
5815
+ return from(getCurrentUser(client, "initial", getRequestErrorHandler, getRequestFailureDiagnostics?.())).pipe(map((currentUser) => ({
5816
+ client,
5817
+ authenticated: !!currentUser?.id,
5818
+ currentUser: currentUser || null
5819
+ })));
5820
+ })) : existingAuthState$).pipe(
5774
5821
  // Cookie state is broadcast to other tabs (and sibling workspaces for the
5775
5822
  // same project in this page) only on meaningful events: post-login probe
5776
5823
  // in handleCallbackUrl, and logout. Broadcasting on every authState$
@@ -5886,6 +5933,10 @@ function _createAuthStore({
5886
5933
  }
5887
5934
  let _didLogOut = !1;
5888
5935
  async function logout() {
5936
+ if (workbenchToken$) {
5937
+ refreshWorkbenchToken2();
5938
+ return;
5939
+ }
5889
5940
  _didLogOut = !0;
5890
5941
  const tokenClient = await firstValueFrom(tokenClient$), stripMiddleware = (c2) => typeof c2.withConfig == "function" ? c2.withConfig({
5891
5942
  _requestHandler: void 0
@@ -5910,7 +5961,7 @@ function _createAuthStore({
5910
5961
  return {
5911
5962
  handleCallbackUrl,
5912
5963
  state: authState$,
5913
- token: tokenStorage.value.pipe(map((t) => t?.token || null)),
5964
+ token: workbenchToken$ ?? tokenStorage.value.pipe(map((t) => t?.token || null)),
5914
5965
  LoginComponent,
5915
5966
  logout
5916
5967
  };
@@ -5919,7 +5970,9 @@ const createAuthStore = memoize$1(
5919
5970
  (options) => _createAuthStore({
5920
5971
  ...options,
5921
5972
  getSessionId: getHashSessionId,
5922
- consumeHashToken
5973
+ consumeHashToken,
5974
+ observeWorkbenchToken,
5975
+ refreshWorkbenchToken
5923
5976
  }),
5924
5977
  // `getRequestErrorHandler` / `getRequestFailureDiagnostics` are functions
5925
5978
  // (not hashable, and not part of the store's identity — they just look up UI
@@ -81381,6 +81434,18 @@ const INITIAL_STATE$1 = {
81381
81434
  error: null,
81382
81435
  loading: !0
81383
81436
  };
81437
+ async function hasPermissionFromAnyGrant(userId, grants, permission, documentValue) {
81438
+ return documentValue ? (await Promise.all(grants.map(async (grant) => {
81439
+ try {
81440
+ const {
81441
+ granted
81442
+ } = await grantsPermissionOn(userId, [grant], permission, documentValue);
81443
+ return granted;
81444
+ } catch {
81445
+ return !1;
81446
+ }
81447
+ }))).some(Boolean) : !0;
81448
+ }
81384
81449
  function useUserListWithPermissions(opts) {
81385
81450
  const $ = c(21), {
81386
81451
  documentValue,
@@ -81408,9 +81473,7 @@ function useUserListWithPermissions(opts) {
81408
81473
  let t42;
81409
81474
  $[15] !== documentValue || $[16] !== permission ? (t42 = async (t52) => {
81410
81475
  const [users_0, groups] = t52, grantPromises = users_0?.map(async (user_0) => {
81411
- const flattenedGrants = [...groups.map((group2) => group2.members?.includes(user_0.id) ? group2.grants : [])].flat(), {
81412
- granted
81413
- } = await grantsPermissionOn(user_0.id, flattenedGrants, permission, documentValue);
81476
+ const flattenedGrants = [...groups.map((group2) => group2.members?.includes(user_0.id) ? group2.grants : [])].flat(), granted = await hasPermissionFromAnyGrant(user_0.id, flattenedGrants, permission, documentValue);
81414
81477
  return {
81415
81478
  ...user_0,
81416
81479
  granted