makearmy-app/middleware.ts

27 lines
819 B
TypeScript
Raw Normal View History

2025-09-26 15:19:19 -04:00
// middleware.ts
import { NextRequest, NextResponse } from "next/server";
2025-09-26 15:19:19 -04:00
/**
* Protect only /my/* pages.
* If the user has no "ma_at" cookie (Directus access token), redirect to /auth/sign-in
* and preserve the original destination via ?next=...
*/
export function middleware(req: NextRequest) {
const token = req.cookies.get("ma_at")?.value;
2025-09-26 15:19:19 -04:00
if (token) {
return NextResponse.next();
}
2025-09-26 15:19:19 -04:00
// Not logged in → send to the correct sign-in route
const url = req.nextUrl.clone();
url.pathname = "/auth/sign-in";
url.searchParams.set("next", req.nextUrl.pathname + req.nextUrl.search);
return NextResponse.redirect(url);
}
2025-09-26 15:19:19 -04:00
// Only run on /my/* so we dont interfere with other routes (including /auth/*)
export const config = {
matcher: ["/my/:path*"],
};