Files
rmcamposandClaude Sonnet 4.6 10938380a0 fix: proxy.ts with correct export format for Next.js 16 Node.js runtime
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>
2026-06-18 20:32:39 -03:00

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).*)"],
};