Replace middleware.ts (Edge Runtime, __dirname crashes Vercel's Edge wrapper) with proxy.ts using `export default async function proxy()` (Node.js runtime). Uses getToken with secureCookie:true in production for correct cookie name (__Secure-authjs.session-token vs authjs.session-token in dev). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
32 lines
867 B
TypeScript
32 lines
867 B
TypeScript
import { type NextRequest, NextResponse } from "next/server";
|
|
import { getToken } from "next-auth/jwt";
|
|
|
|
export default async function proxy(req: NextRequest) {
|
|
const { pathname } = req.nextUrl;
|
|
|
|
const token = await getToken({
|
|
req,
|
|
secret: process.env.AUTH_SECRET,
|
|
secureCookie: process.env.NODE_ENV === "production",
|
|
});
|
|
|
|
const isLoggedIn = !!token;
|
|
const isDashboardRoute = pathname.startsWith("/dashboard");
|
|
const isAuthRoute =
|
|
pathname.startsWith("/login") || pathname.startsWith("/register");
|
|
|
|
if (isDashboardRoute && !isLoggedIn) {
|
|
return NextResponse.redirect(new URL("/login", req.url));
|
|
}
|
|
|
|
if (isAuthRoute && isLoggedIn) {
|
|
return NextResponse.redirect(new URL("/dashboard", req.url));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
|
|
};
|