layout and middleware fix
This commit is contained in:
parent
afebc0843f
commit
7b2b185ed9
2 changed files with 28 additions and 26 deletions
|
|
@ -1,24 +1,26 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
// middleware.ts
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const PROTECTED_PATHS = ["/my", "/api/my"];
|
||||
/**
|
||||
* 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;
|
||||
|
||||
export function middleware(req: NextRequest) {
|
||||
const { pathname } = req.nextUrl;
|
||||
const needsAuth = PROTECTED_PATHS.some(
|
||||
(p) => pathname === p || pathname.startsWith(p + "/")
|
||||
);
|
||||
if (!needsAuth) return NextResponse.next();
|
||||
if (token) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const hasToken = Boolean(req.cookies.get("ma_at")?.value);
|
||||
if (!hasToken) {
|
||||
const url = new URL("/sign-in", req.url);
|
||||
url.searchParams.set("next", pathname);
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!_next|static|favicon.ico).*)"],
|
||||
};
|
||||
// Only run on /my/* so we don’t interfere with other routes (including /auth/*)
|
||||
export const config = {
|
||||
matcher: ["/my/:path*"],
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue