UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

119 lines (117 loc) • 4.47 kB
import { panic } from "../assertions.js"; import { softrConfig } from "../softr-config.js"; import { ACCESS_REASONS, checkRule } from "./visibility.js"; import { setResponseHeader } from "@tanstack/react-start/server"; import { createIsomorphicFn } from "@tanstack/react-start"; import { redirect } from "@tanstack/react-router"; //#region src/route/route.ts /** * Marks the answer of a page a guard refused by throwing, with the reason as its * value. The pod reads it to tell a refusal from a server error, so its name and * values stay as they are. */ const ACCESS_DENIED_HEADER = "x-softr-access-denied"; const markRefused = createIsomorphicFn().server((reason) => { try { setResponseHeader(ACCESS_DENIED_HEADER, reason); } catch {} }); function isAccessReason(value) { return ACCESS_REASONS.some((reason) => reason === value); } /** * True for any access error thrown by a guard, whatever its reason. * * An access error arrives as `unknown` — hydrated from the page, or thrown by * anything else at all — so every field is checked before it is narrowed. */ function isAccessError(error) { if (typeof error !== "object" || error === null) return false; if (!("type" in error) || error.type !== "access_error") return false; return "reason" in error && isAccessReason(error.reason); } function isReason(error, reason) { return isAccessError(error) && error.reason === reason; } /** True for an access error thrown because nobody was signed in. */ function isUnauthenticatedError(error) { return isReason(error, "unauthenticated"); } /** True for an access error thrown because the visitor was in none of the groups. */ function isForbiddenError(error) { return isReason(error, "forbidden"); } /** True for an access error thrown because a signed-out-only route saw a user. */ function isAlreadyAuthenticatedError(error) { return isReason(error, "alreadyAuthenticated"); } /** * Why a guard denied the visitor, for the builder who reads it in the preview. * Plain words: the builder does not write code. */ const ACCESS_DENIED_MESSAGES = { unauthenticated: "This page is only for signed-in users, and nobody is signed in.", forbidden: "You are signed in, but your account is not in any user group this page is open to.", alreadyAuthenticated: "This page is only for signed-out visitors, and you are signed in." }; /** What a guard throws for a reason: a redirect, or an {@link AccessError}. */ function thrownFor(reason, overrides, location) { const behaviour = overrides?.[reason] ?? softrConfig().auth.guard[reason]; const resolved = typeof behaviour === "string" ? { strategy: "redirect", to: behaviour } : behaviour; if (resolved.strategy === "redirect") { if (reason !== "unauthenticated") return redirect({ to: resolved.to }); const { redirectParam } = softrConfig().auth.routes.login; return redirect({ to: resolved.to, search: { [redirectParam]: location.href } }); } const error = { type: "access_error", reason }; markRefused(reason); return error; } function ruleOf(page) { return page.visibility ?? panic("the page states no visibility rule to guard it with"); } /** * A `beforeLoad` handler that denies a visitor the page's rule does not admit. * * Each reason falls back to the app's configured behaviour. Pass `overrides` to * change what any one reason does. A redirected signed-out visitor carries the * page they wanted, for `toReturnPath` to read back. * * Throws a router redirect, so it belongs in `beforeLoad` and nowhere else. * * @example * beforeLoad: guardRoute(pageSettings, { unauthenticated: "/sign-in" }) */ function guardRoute(page, overrides) { return ({ context, location }) => { const result = checkRule(ruleOf(page), context); if (result.ok) return; throw thrownFor(result.reason, overrides, location); }; } /** * Throws what {@link guardRoute} would, for a `beforeLoad` that runs its own * checks after the rule. It takes no overrides. * * @example * beforeLoad: (opts) => { * assertRouteAccess(pageSettings, opts) * if (opts.params.projectId === "demo") throw redirect({ to: "/projects" }) * } */ function assertRouteAccess(page, opts) { const result = checkRule(ruleOf(page), opts.context); if (result.ok) return; throw thrownFor(result.reason, void 0, opts.location); } //#endregion export { ACCESS_DENIED_HEADER, ACCESS_DENIED_MESSAGES, assertRouteAccess, guardRoute, isAccessError, isAlreadyAuthenticatedError, isForbiddenError, isUnauthenticatedError };