26 lines
819 B
TypeScript
26 lines
819 B
TypeScript
// 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 don’t interfere with other routes (including /auth/*)
|
||
export const config = {
|
||
matcher: ["/my/:path*"],
|
||
};
|