import { redirect } from "@tanstack/react-router"; import { safeReturnPath } from "~/lib/return-to"; import type { Viewer } from "~/lib/types"; /** For sign-in, sign-up and recovery pages: someone already signed in goes where they were headed instead. */ export function redirectSignedIn({ context, search, }: { context: { viewer: Viewer | null }; search: { next?: string }; }): void { if (context.viewer) throw redirect({ href: safeReturnPath(search.next) ?? "/", replace: true }); } /** For pages that need an account: signed-out visitors go to sign in and come back here afterwards. */ export function requireSignedIn({ context, location, }: { context: { viewer: Viewer | null }; location: { href: string }; }): void { if (!context.viewer) throw redirect({ to: "/login", search: { next: safeReturnPath(location.href) ?? undefined } }); }