Skip to content

Authorize workspace pages in the request hook instead of the layout - #275

Merged
tnkuehne merged 4 commits into
remote-queries-outside-loadsfrom
workspace-auth-per-docs
Nov 22, 2026
Merged

tnkuehne merged 4 commits into
remote-queries-outside-loadsfrom
workspace-auth-per-docs

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

The workspace layout loaded the principal and organization list on every navigation (depends(url.href)), and page guards read them through await parent(). The load docs note that layout loads do not run on every request and recommend protecting routes in handle or in +page.server.js. The auth docs and remote functions docs add that remote functions must run their own checks, redirecting from queries and forms and returning an error from commands.

Changes:

  • Request hook: For workspace page and data requests, handleSession now checks the organization selection and company SSO after reading the session and before any load runs. Its redirects keep the page as the return path. Form posts without JavaScript return to their page without the action parameter. Remote requests are excluded because remote functions authorize themselves.
  • Remote functions: remoteContext (queries and forms) redirects a caller without access. An ended session goes to sign-in, a required company sign-in to that organization's sign-in, and an unavailable organization selection to the organization chooser. The new commandContext (settings commands and manageWorkflow) returns the same denial as an error, because commands cannot redirect.
  • Page guards: settings/agents, welcome and apps/new call pagePrincipal() instead of await parent(). The legacy /catalog redirect no longer waits for a parent load.
  • Workspace navigation: (workspace)/+layout.server.ts is removed. The layout awaits a new getWorkspace query for the principal and organizations. setMemberRole refreshes it in the same response, and other form submissions invalidate it by default. Analytics identification moves from the root layout into Workspace.svelte.
  • Open tabs: Tabs share one session. When a tab becomes visible, Workspace.svelte refreshes getWorkspace. If another tab switched the organization, the page reloads, which also resets unfinished forms so they cannot submit to the new organization.

Behavior changes:

  • A page whose data comes only from queries does not reach the hook on client-side navigation. If the session ended or the organization selection became unavailable, its query redirects to sign-in or the chooser. A query request does not name its page, so that redirect has no return path. Full page loads and data requests keep it.
  • Another member's change to your role appears when your tab becomes visible again, on reload or after a form submission, instead of on your next click. Authorization is unaffected: remote functions and page loads check the current role on every request.

Tests: the remote endpoint tests now expect a redirect to sign-in or the organization chooser instead of a 401 or 403 error, and still check that no data is returned. Session expiry and an unavailable selection during client navigation expect the redirect without a return path in the JavaScript variant. The role change test reloads the page. New coverage: an open tab reloads with an empty form after another tab switches organizations.

No visible UI change: the error page is unchanged from main.

Validation:

  • pnpm check (typecheck, oxfmt --check, oxlint --type-aware --deny-warnings)
  • Browser suite (test:e2e --grep-invert cli-login.spec.ts) after rebasing on Switch organizations from the sidebar menu #270: 86 passed, 1 skipped
  • handled-errors.integration.test.ts: 14 passed

Stacked on #273.

RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

The PR appears safe to merge; no new blocking issue was found.

Summary

Moves workspace authorization from the server layout into handleSession and loads workspace navigation through getWorkspace().

  • Workspace page requests check access before their page loads run.
  • Remote calls send denied queries and forms to the right access page.
  • Workspace navigation refreshes when roles or organizations change.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Browser request] --> B[handleSession reads session]
  B --> C{Workspace page or data request?}
  C -->|Yes| D[pagePrincipal checks access]
  D -->|Denied| E[Redirect with return path]
  D -->|Allowed| F[Run page loads]
  C -->|Remote request| G[Remote function checks access]
  G --> H{Function kind}
  H -->|Query or form| I[Redirect when denied]
  H -->|Command| J[Return error when denied]
Loading

Reviews (9) · Last reviewed commit: "Redirect signed-out remote calls instead..." · Reviewed by Greptile

@tnkuehne
tnkuehne added this pull request to stack #276 November 21, 2026 19:02
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 05a5fc6 Nov 22 2026, 12:02 AM

The workspace layout loaded the principal and organization list on every
navigation, and page guards read them through await parent(). SvelteKit
does not rerun layout loads on every request and recommends protecting
routes in handle or in page loads, with remote functions authorizing
themselves.

The request hook now checks the organization selection and company SSO for
workspace page and data requests, before any load runs, so its redirects
keep the page as the return path. Page guards call pagePrincipal()
directly. The workspace navigation reads its principal and organizations
from a getWorkspace query, which role changes refresh in the same response
and other form submissions invalidate.

A query that rejects an expired session during client-side navigation now
shows a session-ended error page whose sign-in link returns to that page.
Tabs share one session, so another tab can switch the organization while
this tab keeps the previous workspace. Recheck the workspace query when the
tab becomes visible and reload page data if the organization changed.

Queries reject a company SSO requirement or an unavailable organization
selection with a code. The error page continues through a full page load,
where the request hook redirects to company sign-in or the organization
chooser and keeps the page as the return path.
Refreshing the workspace query changed the organization ID that open forms
submit, while their entered values stayed in place. A form prepared for one
organization could then pass the selection check and write to the other.

Reload the page when a returning tab finds a different organization, which
resets unfinished forms. A role change within the same organization still
only refreshes the navigation.
Queries and forms now redirect a caller without access, as the
SvelteKit docs describe for remote functions: an ended session goes to
sign-in, a required company sign-in to that organization's sign-in,
and an unavailable organization selection to the organization choice.
Commands cannot redirect and keep returning the error. The error page
loses its session and organization states again.

A query request does not name its page, so after a client-side
navigation these destinations have no return path. Full page loads
still pass through the request hook, which keeps it. Form posts
without JavaScript now return to their page without the action
parameter.
@tnkuehne
tnkuehne force-pushed the workspace-auth-per-docs branch from 749f49c to 05a5fc6 Compare November 22, 2026 00:01
@tnkuehne
tnkuehne merged commit 4b0728c into main Nov 22, 2026
17 checks passed
@tnkuehne
tnkuehne deleted the workspace-auth-per-docs branch November 22, 2026 00:36
tnkuehne added a commit that referenced this pull request Nov 22, 2026
Workspace pages now start with the organization's slug, for example
`/acme/apps/<id>`. Before, the active organization lived only in the
browser session and was shared by every tab. A shared link opened the
receiver's active organization, so an app from another organization
showed "not found". Two organizations could not be open side by side:
another tab's switch made open forms fail with a 409 and reloaded the
page.

Changes:
- **Routes:** All workspace routes move under
`(workspace)/[organization=organization]/`. The `organization` matcher
(`src/params.ts`) accepts slug-shaped segments only, so other paths
still 404. Static top-level pages win over the parameter.
`reservedOrganizationSlugs` in `@platform/contracts` keeps new
organizations from taking their names, and a test fails when a new
top-level route is missing from the list.
- **Page authorization:** The request hook resolves the organization
named by the URL (`identity.member` with `organizationSlug`) before any
load runs, as introduced in #275. An organization the person cannot open
looks the same as one that does not exist. The new
`[organization]/+layout.server.ts` turns it into a 404 rendered by
`+error.svelte`, because errors thrown in `handle` only reach the
fallback page.
- **Remote functions:** Every workspace query, form and command now
receives `organizationId` as an argument. The [remote functions
docs](https://svelte.dev/docs/kit/remote-functions#Using-getRequestEvent)
state that queries are cached by their arguments and cannot read
`params` or `url`. `remoteContext` and `commandContext` resolve that
organization directly, so the stale-selection check (409 "Your
organization selection changed") is gone. Pages read the ID from the
workspace context. Only `getWorkspace` takes the slug, and its result
supplies the ID.
- **No session organization:** The session no longer stores an
organization. `/` and `/welcome` open the member's only organization or
the chooser (`openOrganization`). The chooser's entries are links:
destinations chosen before the organization is known name it with an
`[organization]` placeholder, such as `/[organization]/welcome`, which
the chooser fills in (`chosenDestination`). MCP sign-in returns from the
chooser with `organization=<slug>` in its address and asks again for an
organization the person cannot open. `PUT /api/v1/organization` and
`identity.selectOrganization` are removed; the CLI never used them, it
always sends `X-Widefleet-Organization`. Better Auth's organization
plugin keeps its `activeOrganizationId` column, which Widefleet no
longer reads.
- **API without a header:** Requests name their organization with
`X-Widefleet-Organization`; without it, the extension's default or the
member's only organization applies, as for tokens. Browsers cannot send
that header for images, so workspace pages load app icons from
`/<slug>/apps/<id>/icon`, which the request hook authorizes like the
page. `/api/v1/apps/<id>/icon` keeps header-based authentication for API
clients. Template icons, `GET /api/v1/reporting/status` and `POST
/api/v1/reporting/errors` serve the whole installation and only require
a signed-in person. Error reports from pages and the API name their
organization.
- **Navigation:** Every link and server redirect goes through
`workspacePath(slug, path)` or the context's `path()`. The organization
menu and the chooser now use plain links instead of selection forms, the
menu switches without reloading the page, and an open tab no longer
reloads when another tab switches organizations. The app tabs read the
active tab from the route ID instead of a fixed URL segment.
- **Other entry points:** Personal connection completion returns to
`/<slug>/connections`, and `GET /api/v1/account` (`widefleet account`)
returns `/<slug>/account`. Recovery opens the installation
organization's company sign-in. A newly created organization lands on
its own welcome page, and an accepted invitation opens its organization.
The legacy `/catalog` redirect is removed; `/<slug>/settings` still
opens its first section.

Behavior changes:
- Addresses without an organization, such as `/apps`, now 404. Nobody
depends on the old addresses yet, so this adds no redirects.
- Anonymous requests to any slug-shaped path redirect to sign-in, like
other signed-in pages. Paths that cannot be a slug (`/missing.html`)
still 404.
- People with several organizations choose one at `/` instead of
returning to the last one.
- Browser requests to `/api/v1/...` with several organizations and no
header return `403 ORGANIZATION_REQUIRED`, like token requests.
- Server-side error reports from remote functions no longer carry an
organization group, because the organization belongs to each call while
`locals` belongs to the whole request. Page and API errors keep it.

Tests: `organization-selection.integration.test.ts` covers page
resolution by slug, scoping by header, the header requirement with a
second organization and that a stored session value is ignored. The
browser suite covers parallel tabs in two organizations, creating apps
in the organization a tab shows, app links whatever the session holds,
404s for foreign organizations, the sidebar switch without a page
reload, the start-page chooser, external destinations in the chooser,
MCP sign-in with the chosen organization and app icons from the
workspace address (served for members, 404 for other organizations,
sign-in for anonymous requests). The remaining specs use
`/widefleet/...` for the installation organization. New unit test:
`organization-paths.test.ts`.

UI: layouts and components are unchanged; only addresses differ. The
chooser's entries became links with the same look. The screenshots below
show the same steps on `main` and on this branch, with the address shown
at the bottom of each image.

| View | `main` | This branch |
| --- | --- | --- |
| Home after choosing Acme Logistics, 1280 px, light | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-home-1280-light.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-home-1280-light.png"
width="360"> |
| Home after choosing Acme Logistics, 1280 px, dark | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-home-1280-dark.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-home-1280-dark.png"
width="360"> |
| Home after choosing Acme Logistics, 390 px, light | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-home-390-light.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-home-390-light.png"
width="360"> |
| Home after choosing Acme Logistics, 390 px, dark | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-home-390-dark.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-home-390-dark.png"
width="360"> |
| Organization menu, 1280 px, light | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-menu-1280-light.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-menu-1280-light.png"
width="360"> |
| Organization menu, 1280 px, dark | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-menu-1280-dark.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-menu-1280-dark.png"
width="360"> |
| My apps after switching to Globex Manufacturing, 1280 px, light | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-apps-1280-light.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-apps-1280-light.png"
width="360"> |
| My apps after switching to Globex Manufacturing, 1280 px, dark | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-apps-1280-dark.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-apps-1280-dark.png"
width="360"> |
| My apps after switching to Globex Manufacturing, 390 px, light | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-apps-390-light.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-apps-390-light.png"
width="360"> |
| My apps after switching to Globex Manufacturing, 390 px, dark | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/before-apps-390-dark.png"
width="360"> | <img
src="https://gist.githubusercontent.com/tnkuehne/05b53e73b41d72c0ba7a60e1a9198af3/raw/74c197bb68fb5df824504c224167a75199212a31/after-apps-390-dark.png"
width="360"> |

Checked: home, organization menu and My apps at 1280 and 390 px in light
and dark, switching with the menu (desktop) and the chooser (mobile).
Not checked visually: the 404 page for a foreign organization (unchanged
`+error.svelte`, covered by the browser suite).

Validation:
- `pnpm check`
- `pnpm exec vitest run`: 130 files passed, 44 skipped (1240 tests)
- Browser suite (`test:e2e --grep-invert cli-login.spec.ts`) against a
fresh `pnpm build`: 91 passed, 1 skipped (source suite, gated)
- Cloud consumer: typecheck, build and tests against this branch:
typecheck and build pass; 12 test files, 187 tests pass

Docs: #284 (draft, waits for a release with this change).




















<!-- greptile_comment -->

<!-- greptile_summary -->

<h2><a
href="https://app.greptile.com/api/retrigger?id=115480825&amp;sig=20f1c81e1c7e60ca4f53ddfd336ec58e5269f9344c6ae5baf238bf3cef38256a"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img
alt="Retrigger"
src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"
align="right"></picture></a><a
href="https://app.greptile.com/-/switch-tenant?tenant=mendra&amp;next=%2Fwidefleet%2Fwidefleet%2Fpull%2F283&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img
alt="View in Greptile"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"
align="right"></picture></a>Confidence Score: 5/5</h2>

<!-- greptile-impact-verdict --> The PR appears safe to merge; no new
blocking issue remains.

<details open><summary><h3>Summary</h3></summary>

Workspace pages now name their organization in the URL. Remote calls
receive an explicit `organizationId`, so tabs no longer share an
organization selection.

- Workspace pages open the organization named in their address.
- The organization chooser opens the selected slug-based workspace.
- API requests and browser icons use the organization each request
names.
- External sign-in and return links keep the organization's workspace
address.
</details>

<!-- greptile_confidence_score:5 -->

<sub>Reviews (10) · Last reviewed commit: ["Drop the unused session ID
from
authenti..."](563d085)
· [Reviewed by
Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub>

<!-- /greptile_comment -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant