makearmy-app/middleware.ts

135 lines
5.9 KiB
TypeScript
Raw Normal View History

2025-09-26 15:19:19 -04:00
// middleware.ts
2025-09-27 14:30:16 -04:00
import { NextResponse, NextRequest } from "next/server";
2025-09-27 17:59:52 -04:00
const PUBLIC_PATHS = new Set<string>(["/auth/sign-in", "/auth/sign-up"]);
/** Helper: are we about to redirect to the same URL? */
function isSameUrl(req: NextRequest, mapped: MapResult) {
const dest = new URL(req.url);
dest.pathname = mapped.pathname;
if (mapped.query) {
// start from existing params so we preserve any others
for (const [k, v] of Object.entries(mapped.query)) dest.searchParams.set(k, v);
}
return dest.href === req.url;
}
2025-09-27 14:30:16 -04:00
export function middleware(req: NextRequest) {
2025-09-27 15:37:05 -04:00
const url = req.nextUrl.clone();
const { pathname } = url;
2025-09-27 16:52:05 -04:00
// ── 1) Legacy → Portal / Canonical mapping (runs before auth gating)
2025-09-27 15:37:05 -04:00
const mapped = legacyMap(pathname);
2025-09-27 17:59:52 -04:00
if (mapped && !isSameUrl(req, mapped)) {
// Build destination on the same URL object to keep host/proto
2025-09-27 15:37:05 -04:00
url.pathname = mapped.pathname;
if (mapped.query) {
for (const [k, v] of Object.entries(mapped.query)) {
url.searchParams.set(k, v);
}
}
return NextResponse.redirect(url);
}
// ── 2) Auth gating
2025-09-27 14:30:16 -04:00
const token = req.cookies.get("ma_at")?.value ?? "";
2025-09-27 15:37:05 -04:00
const isAuthRoute = pathname.startsWith("/auth/");
2025-09-27 15:37:05 -04:00
// Authed users on any /auth/* route → /portal
2025-09-27 14:30:16 -04:00
if (token && isAuthRoute) {
url.pathname = "/portal";
url.search = "";
return NextResponse.redirect(url);
2025-09-26 15:34:24 -04:00
}
2025-09-27 15:37:05 -04:00
// Unauthed users on protected paths → sign-in (no ?next=)
2025-09-27 14:41:56 -04:00
if (!token && !isPublicPath(pathname)) {
2025-09-27 14:30:16 -04:00
url.pathname = "/auth/sign-in";
2025-09-27 15:37:05 -04:00
url.search = "";
2025-09-27 14:30:16 -04:00
return NextResponse.redirect(url);
2025-09-26 15:34:24 -04:00
}
return NextResponse.next();
}
2025-09-27 15:37:05 -04:00
type MapResult = { pathname: string; query?: Record<string, string> };
function legacyMap(pathname: string): MapResult | null {
2025-09-27 17:59:52 -04:00
// If were already inside the portal, dont try to remap again.
if (pathname.startsWith("/portal")) return null;
// 1) DETAIL PAGES: map legacy detail URLs straight into the portal with ?id=
2025-09-27 18:30:18 -04:00
// NOTE: We intentionally DO NOT remap `/lasers/:id` and `/projects/:id`
// so the portal iframes can load those canonical pages without recursion.
2025-09-27 16:52:05 -04:00
const detailRules: Array<[RegExp, (m: RegExpExecArray) => MapResult]> = [
2025-09-27 17:59:52 -04:00
// Laser settings
[/^\/fiber-settings\/([^/]+)\/?$/i, (m) => ({ pathname: "/portal/laser-settings", query: { t: "fiber", id: m[1] } })],
[/^\/uv-settings\/([^/]+)\/?$/i, (m) => ({ pathname: "/portal/laser-settings", query: { t: "uv", id: m[1] } })],
[/^\/co2-galvo-settings\/([^/]+)\/?$/i, (m) => ({ pathname: "/portal/laser-settings", query: { t: "co2-galvo", id: m[1] } })],
[/^\/co2-gantry-settings\/([^/]+)\/?$/i, (m) => ({ pathname: "/portal/laser-settings", query: { t: "co2-gantry", id: m[1] } })],
2025-09-27 18:30:18 -04:00
[/^\/co2gantry-settings\/([^/]+)\/?$/i, (m) => ({ pathname: "/portal/laser-settings", query: { t: "co2-gantry", id: m[1] } })],
2025-09-27 17:59:52 -04:00
// Materials
[/^\/materials\/([^/]+)\/?$/i, (m) => ({ pathname: "/portal/materials", query: { t: "materials", id: m[1] } })],
[/^\/materials-coatings\/([^/]+)\/?$/i, (m) => ({ pathname: "/portal/materials", query: { t: "materials-coatings", id: m[1] } })],
2025-09-27 18:30:18 -04:00
// (no lasers/projects detail remap here on purpose)
2025-09-27 16:52:05 -04:00
];
for (const [re, to] of detailRules) {
const m = re.exec(pathname);
if (m) return to(m);
2025-09-27 15:37:05 -04:00
}
2025-09-27 16:52:05 -04:00
2025-09-27 17:59:52 -04:00
// 2) LIST PAGES: legacy lists → portal lists (with tab param) or sections
2025-09-27 16:52:05 -04:00
const listRules: Array<[RegExp, MapResult]> = [
2025-09-27 17:59:52 -04:00
// Laser settings lists
[/^\/fiber-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "fiber" } }],
[/^\/uv-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "uv" } }],
[/^\/co2-galvo-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-galvo" } }],
[/^\/co2-ganry-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } }], // typo catch
[/^\/co2-gantry-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } }],
[/^\/co2gantry-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } }], // old alias
// Materials lists
[/^\/materials\/?$/i, { pathname: "/portal/materials", query: { t: "materials" } }],
[/^\/materials\/materials\/?$/i, { pathname: "/portal/materials", query: { t: "materials" } }],
2025-09-27 16:52:05 -04:00
[/^\/materials\/materials-coatings\/?$/i,
{ pathname: "/portal/materials", query: { t: "materials-coatings" } }],
2025-09-27 17:59:52 -04:00
[/^\/materials-coatings\/?$/i, { pathname: "/portal/materials", query: { t: "materials-coatings" } }],
2025-09-27 16:52:05 -04:00
2025-09-27 17:59:52 -04:00
// Other lists
[/^\/lasers\/?$/i, { pathname: "/portal/laser-sources" }],
[/^\/projects\/?$/i, { pathname: "/portal/projects" }],
[/^\/my\/rigs\/?$/i, { pathname: "/portal/rigs", query: { t: "my" } }],
2025-09-27 16:52:05 -04:00
];
for (const [re, dest] of listRules) {
if (re.test(pathname)) return dest;
}
return null;
2025-09-27 15:37:05 -04:00
}
2025-09-27 14:30:16 -04:00
function isPublicPath(pathname: string): boolean {
if (PUBLIC_PATHS.has(pathname)) return true;
2025-09-27 14:41:56 -04:00
// Static assets / internals
2025-09-27 14:30:16 -04:00
if (
pathname.startsWith("/_next/") ||
pathname.startsWith("/static/") ||
pathname.startsWith("/images/") ||
pathname === "/favicon.ico" ||
pathname === "/robots.txt" ||
pathname === "/sitemap.xml"
) return true;
2025-09-27 17:59:52 -04:00
// API routes arent gated here; each route should enforce auth as needed
2025-09-27 14:30:16 -04:00
if (pathname.startsWith("/api/")) return true;
// Everything else is protected
return false;
}
2025-09-26 15:34:24 -04:00
export const config = {
2025-09-27 17:59:52 -04:00
matcher: ["/((?!_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml|images|static).*)"],
2025-09-26 15:34:24 -04:00
};