feat(01-01): configure Auth.js v5 JWT + Credentials, route protection middleware
- Created src/lib/auth.ts with NextAuth JWT strategy, 7-day rolling session, Credentials provider - Created src/app/api/auth/[...nextauth]/route.ts with GET/POST handlers and force-dynamic - Created middleware.ts at project root (not src/) protecting /agent/* routes - Fixed db/index.ts: lazy Proxy singleton prevents neon() crash during Next.js build - npm run build passes; /api/auth/[...nextauth] renders as Dynamic route
This commit is contained in:
25
teressa-copeland-homes/middleware.ts
Normal file
25
teressa-copeland-homes/middleware.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { auth } from "@/lib/auth";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export default auth((req) => {
|
||||
const isLoggedIn = !!req.auth;
|
||||
const isAgentRoute = req.nextUrl.pathname.startsWith("/agent");
|
||||
const isLoginPage = req.nextUrl.pathname === "/agent/login";
|
||||
|
||||
// Protect all /agent/* routes except the login page itself
|
||||
if (isAgentRoute && !isLoginPage && !isLoggedIn) {
|
||||
return NextResponse.redirect(new URL("/agent/login", req.nextUrl.origin));
|
||||
}
|
||||
|
||||
// Redirect already-authenticated users away from the login page
|
||||
if (isLoginPage && isLoggedIn) {
|
||||
return NextResponse.redirect(new URL("/agent/dashboard", req.nextUrl.origin));
|
||||
}
|
||||
});
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
// Run on /agent/* routes, excluding Next.js internals and static files
|
||||
"/((?!_next/static|_next/image|favicon.ico).*)",
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user