/** * 纯函数路由守卫 * * 不依赖 React/Next.js,可被 `src/proxy.ts`(边缘运行时)与 * `src/lib/auth/use-auth-gate.tsx`(客户端 hook)共用。 * * 对齐 Flutter `lib/router/app_router.dart` 中 `_handleAuthRedirect` 的语义: * 已登录用户访问 `/splash` 或 `/auth` 时,重定向到 `/chat`。 */ import { AUTH_ONLY_ROUTES, ROUTES, type StaticRoute } from "../routes"; /** * 判定路径是否为"仅未登录态可见"的入口路由。 */ export function isAuthOnlyRoute(pathname: string): pathname is StaticRoute { return (AUTH_ONLY_ROUTES as readonly string[]).includes(pathname); } /** * 判定路径是否为公开路由(游客态也允许访问)。 */ export function isPublicRoute(pathname: string): pathname is StaticRoute { return pathname === ROUTES.chat || pathname === ROUTES.sidebar; } /** * 已登录用户访问入口路由时返回应跳转到的目标;其余情况返回 null。 * * 当前实现仅做"splash/auth → chat"的重定向;未来若新增入口路由(如 onboarding), * 只需在此函数追加分支。 */ export function resolveAuthedRedirect(pathname: string): string | null { if (isAuthOnlyRoute(pathname)) return ROUTES.chat; return null; }