auth-cookies build error fix
This commit is contained in:
parent
7b2b185ed9
commit
514982d009
4 changed files with 213 additions and 133 deletions
|
|
@ -1,26 +1,32 @@
|
|||
// middleware.ts
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } 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;
|
||||
export function middleware(req: NextRequest) {
|
||||
const { pathname, searchParams, origin } = req.nextUrl;
|
||||
|
||||
if (token) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
const isAuthPage =
|
||||
pathname === "/auth/sign-in" || pathname === "/auth/sign-up";
|
||||
const isMyArea = pathname.startsWith("/my/");
|
||||
|
||||
// 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);
|
||||
}
|
||||
const at = req.cookies.get("ma_at")?.value;
|
||||
|
||||
// Only run on /my/* so we don’t interfere with other routes (including /auth/*)
|
||||
export const config = {
|
||||
matcher: ["/my/:path*"],
|
||||
};
|
||||
// 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"],
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue