makearmy-app/middleware.ts

26 lines
819 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// middleware.ts
import { NextRequest, NextResponse } from "next/server";
/**
* 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;
if (token) {
return NextResponse.next();
}
// 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);
}
// Only run on /my/* so we dont interfere with other routes (including /auth/*)
export const config = {
matcher: ["/my/:path*"],
};