import { getDefaultServer, getServer } from "@/utils/storage/servers"; import { getUser } from "@/utils/storage/user"; import { createFileRoute, redirect } from "@tanstack/react-router"; /** * Session bootstrap is deliberately local-only: reading the Tauri store is * quick, while network verification would block the first render. The token * is validated in the background by ProviderWrapper, which redirects to the * login page on a real 401/403 without punishing transient network errors. */ export const Route = createFileRoute("/_api")({ beforeLoad: async ({ context, location }) => { const isAuthPage = location.pathname.startsWith("/login") || location.pathname.startsWith("/setup") || location.pathname.startsWith("/error"); if (!context.api) { const currentServerId = await getDefaultServer(); if (!currentServerId) { if (!isAuthPage) throw redirect({ to: "/setup/server/add" }); return; } const currentServer = await getServer(currentServerId); if (!currentServer?.address) { if (!isAuthPage) throw redirect({ to: "/setup/server/add" }); return; } const userOnDisk = await getUser(); context.createApi(currentServer.address, userOnDisk?.AccessToken); if (!userOnDisk?.AccessToken && !isAuthPage) { throw redirect({ to: "/login/manual", search: { redirect: location.href }, }); } } else if (!context.api.accessToken && !isAuthPage) { throw redirect({ to: "/login/manual", search: { redirect: location.href }, }); } }, });