refactor: separate authentication routes from public routes and adjust middleware redirection logic.
This commit is contained in:
@@ -1,14 +1,21 @@
|
|||||||
import { auth } from "@/auth";
|
import { auth } from "@/auth";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
// Public routes that don't require authentication
|
// Auth routes - logged-in users should be redirected away from these
|
||||||
const publicRoutes = ["/login", "/signup", "/forgot-password", "/reset-password", "/verify-email", "/contact"];
|
const authRoutes = ["/login", "/signup", "/forgot-password", "/reset-password", "/verify-email"];
|
||||||
|
|
||||||
|
// Public routes - accessible to everyone (logged in or not)
|
||||||
|
const publicRoutes = ["/", "/contact", "/about", "/faq"];
|
||||||
|
|
||||||
export default auth((req) => {
|
export default auth((req) => {
|
||||||
const { nextUrl } = req;
|
const { nextUrl } = req;
|
||||||
const isLoggedIn = !!req.auth;
|
const isLoggedIn = !!req.auth;
|
||||||
const userRole = (req.auth?.user as any)?.role;
|
const userRole = (req.auth?.user as any)?.role;
|
||||||
|
|
||||||
|
const isAuthRoute = authRoutes.some(
|
||||||
|
(route) => nextUrl.pathname === route || nextUrl.pathname.startsWith(route + "/")
|
||||||
|
);
|
||||||
|
|
||||||
const isPublicRoute = publicRoutes.some(
|
const isPublicRoute = publicRoutes.some(
|
||||||
(route) => nextUrl.pathname === route || nextUrl.pathname.startsWith(route + "/")
|
(route) => nextUrl.pathname === route || nextUrl.pathname.startsWith(route + "/")
|
||||||
);
|
);
|
||||||
@@ -26,14 +33,19 @@ export default auth((req) => {
|
|||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect logged-in users away from public routes (login, signup)
|
// Allow access to public routes for everyone
|
||||||
if (isLoggedIn && isPublicRoute) {
|
if (isPublicRoute) {
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redirect logged-in users away from auth routes (login, signup, etc.)
|
||||||
|
if (isLoggedIn && isAuthRoute) {
|
||||||
// Redirect to home page instead of dashboard
|
// Redirect to home page instead of dashboard
|
||||||
return NextResponse.redirect(new URL("/", nextUrl.origin));
|
return NextResponse.redirect(new URL("/", nextUrl.origin));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect non-logged-in users to login page for protected routes
|
// Redirect non-logged-in users to login page for protected routes
|
||||||
if (!isLoggedIn && !isPublicRoute) {
|
if (!isLoggedIn && !isAuthRoute) {
|
||||||
const loginUrl = new URL("/login", nextUrl.origin);
|
const loginUrl = new URL("/login", nextUrl.origin);
|
||||||
loginUrl.searchParams.set("callbackUrl", nextUrl.pathname);
|
loginUrl.searchParams.set("callbackUrl", nextUrl.pathname);
|
||||||
return NextResponse.redirect(loginUrl);
|
return NextResponse.redirect(loginUrl);
|
||||||
|
|||||||
Reference in New Issue
Block a user