124_webcalibre2/src/proxy.ts

30 lines
860 B
TypeScript
Raw Normal View History

2026-01-06 09:24:25 +00:00
import { NextResponse } from "next/server";
import { auth } from "@/auth";
const protectedRoutes = ["/dashboard", "/profile"];
const apiAuthPrefix = "/api/auth";
export const proxy = auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
const path = nextUrl.pathname;
const isApiAuthRoute = path.startsWith(apiAuthPrefix);
const isProtectedRoute = protectedRoutes.includes(path);
// 1. Allow API Auth calls (Login/Logout/Callback)
if (isApiAuthRoute) {
return NextResponse.next();
}
// 2. CHANGED: Redirect to HOME (/) instead of /login if logged out
if (isProtectedRoute && !isLoggedIn) {
return NextResponse.redirect(new URL("/", nextUrl));
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};