middleware update for redirects
This commit is contained in:
parent
049f5fb841
commit
f40f9a4092
1 changed files with 59 additions and 10 deletions
|
|
@ -4,34 +4,83 @@ import { NextResponse, NextRequest } from "next/server";
|
|||
const PUBLIC_PATHS = new Set<string>([
|
||||
"/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<string, string> };
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue