@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
39 lines (37 loc) • 1.36 kB
JavaScript
import { softrConfig } from "../softr-config.js";
//#region src/auth/returnPath.ts
/**
* Where to send a visitor who just signed in: the path a guard sent them away
* from, else where `auth.guard.alreadyAuthenticated` in softr.json points, else
* `/`. Anything but a path on this app counts as missing. It always reloads the
* page, so the router picks up the new session.
*
* @example
* const location = useLocation()
* navigate(toReturnPath(location))
*/
function toReturnPath(location) {
const { auth } = softrConfig();
const target = location.search[auth.routes.login.redirectParam];
return {
href: resolveReturnPath(target),
reloadDocument: true
};
}
/**
* `target` when it is a path on this app, else where
* `auth.guard.alreadyAuthenticated` in softr.json points, else `/`.
*/
function resolveReturnPath(target) {
if (isAppPath(target)) return target;
const fallback = softrConfig().auth.guard.alreadyAuthenticated;
if (typeof fallback === "string") return fallback;
if (fallback.strategy === "redirect") return fallback.to;
return "/";
}
/** It starts with `/` and names no other host, however that is spelled. */
function isAppPath(target) {
return typeof target === "string" && target.startsWith("/") && new URL(target, "https://app.invalid").origin === "https://app.invalid";
}
//#endregion
export { resolveReturnPath, toReturnPath };