30 lines
860 B
TypeScript
30 lines
860 B
TypeScript
|
|
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).*)"],
|
||
|
|
};
|