makearmy-app/middleware.ts

33 lines
980 B
TypeScript
Raw Normal View History

2025-09-26 15:19:19 -04:00
// middleware.ts
2025-09-26 15:34:24 -04:00
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
2025-09-26 15:34:24 -04:00
export function middleware(req: NextRequest) {
const { pathname, searchParams, origin } = req.nextUrl;
2025-09-26 15:34:24 -04:00
const isAuthPage =
pathname === "/auth/sign-in" || pathname === "/auth/sign-up";
const isMyArea = pathname.startsWith("/my/");
2025-09-26 15:34:24 -04:00
const at = req.cookies.get("ma_at")?.value;
2025-09-26 15:34:24 -04:00
// Gate /my/*
if (isMyArea && !at) {
const dest = new URL("/auth/sign-in", origin);
dest.searchParams.set("next", pathname + (req.nextUrl.search || ""));
return NextResponse.redirect(dest);
}
// If logged in and on auth pages, send to next or /my/rigs
if (isAuthPage && at) {
const nxt = searchParams.get("next") || "/my/rigs";
return NextResponse.redirect(new URL(nxt, origin));
}
return NextResponse.next();
}
export const config = {
matcher: ["/my/:path*", "/auth/sign-in", "/auth/sign-up"],
};