@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
95 lines (93 loc) • 3.42 kB
JavaScript
import { panic } from "../assertions.js";
import { softrConfig } from "../softr-config.js";
import { ACCESS_REASONS, checkRule } from "./visibility.js";
import { redirect } from "@tanstack/react-router";
//#region src/route/route.ts
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");
}
/** 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 }
});
}
return {
type: "access_error",
reason
};
}
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 { assertRouteAccess, guardRoute, isAccessError, isAlreadyAuthenticatedError, isForbiddenError, isUnauthenticatedError };