From f40f9a4092da36c66c188350f679fe59da70124b Mon Sep 17 00:00:00 2001 From: makearmy Date: Sat, 27 Sep 2025 15:37:05 -0400 Subject: [PATCH] middleware update for redirects --- middleware.ts | 69 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/middleware.ts b/middleware.ts index 53cd0e3b..32006c72 100644 --- a/middleware.ts +++ b/middleware.ts @@ -4,34 +4,83 @@ import { NextResponse, NextRequest } from "next/server"; const PUBLIC_PATHS = new Set([ "/auth/sign-in", "/auth/sign-up", - // add oauth/callback endpoints here if you use them, e.g.: "/auth/callback" ]); export function middleware(req: NextRequest) { - const { pathname } = req.nextUrl; - const isAuthRoute = pathname.startsWith("/auth/"); - const token = req.cookies.get("ma_at")?.value ?? ""; + const url = req.nextUrl.clone(); + const { pathname } = url; - // If already authed and hitting an auth route, always go to the portal + // ── 1) Legacy → Portal mapping (runs before auth gating) + const mapped = legacyMap(pathname); + if (mapped) { + url.pathname = mapped.pathname; + // keep existing query, add/override provided params (e.g., ?t=fiber) + if (mapped.query) { + for (const [k, v] of Object.entries(mapped.query)) { + url.searchParams.set(k, v); + } + } + return NextResponse.redirect(url); + } + + // ── 2) Auth gating + const token = req.cookies.get("ma_at")?.value ?? ""; + const isAuthRoute = pathname.startsWith("/auth/"); + + // Authed users on any /auth/* route → /portal if (token && isAuthRoute) { - const url = req.nextUrl.clone(); url.pathname = "/portal"; url.search = ""; return NextResponse.redirect(url); } - // If not authed and path is protected → send to sign-in (no ?next=) + // Unauthed users on protected paths → sign-in (no ?next=) if (!token && !isPublicPath(pathname)) { - const url = req.nextUrl.clone(); url.pathname = "/auth/sign-in"; - url.search = ""; // IMPORTANT: drop next so login always goes to /portal + url.search = ""; return NextResponse.redirect(url); } return NextResponse.next(); } -// Helpers +type MapResult = { pathname: string; query?: Record }; + +function legacyMap(pathname: string): MapResult | null { + switch (pathname) { + // Laser settings (old links) + case "/fiber-settings": + return { pathname: "/portal/laser-settings", query: { t: "fiber" } }; + case "/uv-settings": + return { pathname: "/portal/laser-settings", query: { t: "uv" } }; + case "/co2-galvo-settings": + return { pathname: "/portal/laser-settings", query: { t: "co2-galvo" } }; + case "/co2-gantry-settings": + return { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } }; + + // Materials (both legacy structures) + case "/materials": + return { pathname: "/portal/materials", query: { t: "materials" } }; + case "/materials/materials": + return { pathname: "/portal/materials", query: { t: "materials" } }; + case "/materials/materials-coatings": + return { pathname: "/portal/materials", query: { t: "materials-coatings" } }; + case "/materials-coatings": + return { pathname: "/portal/materials", query: { t: "materials-coatings" } }; + + // Lasers / Projects / Rigs (legacy) + case "/lasers": + return { pathname: "/portal/laser-sources" }; + case "/projects": + return { pathname: "/portal/projects" }; + case "/my/rigs": + return { pathname: "/portal/rigs", query: { t: "my" } }; + + default: + return null; + } +} + function isPublicPath(pathname: string): boolean { if (PUBLIC_PATHS.has(pathname)) return true;