Site / foldkit-site

Guarding which routes a visitor sees

Route access rules control presentation — redirecting an unauthenticated visitor to /sign-in instead of flashing an empty screen. Always enforce data authorization on the server with foldkit-guard and RemoteServer.

Attach access to a node to specify who may see it and its descendants, plus where anyone else should go:

ts
const Projects = Site.node(pipe(literal('projects'), mapTo(AppRoute.Projects)), AppRoute.Projects)
  .access<Me>({ allow: me => me.signedIn, otherwise: toSignIn })
  .node

Call Site.accessOf(site, route, who) and branch with Access.match:

  • Allowed: Render the route.
  • Pending: who is Option.none() (still loading); show a loading state instead of flashing sign-in.
  • Denied: Replace the URL with otherwise(requested) in the browser, or return a 303 redirect on the server. Mounting the otherwise target inside a subtree protected by the same rule throws at runtime to prevent redirect loops.