feat: Centralize authentication redirects to middleware, remove client-side redirect logic, and refine logout process.

This commit is contained in:
pradeepkumar
2026-03-10 09:28:19 +05:30
parent 69eff70fbb
commit c7f1b89203
9 changed files with 77 additions and 70 deletions

View File

@@ -7,6 +7,9 @@ const authRoutes = ["/login", "/signup", "/forgot-password", "/reset-password",
// Public routes - accessible to everyone (logged in or not)
const publicRoutes = ["/", "/contact", "/about", "/faq", "/education", "/logout", "/user/dashboard", "/user/profiles", "/user/profile"];
// Routes that should NEVER be redirected away from (even if logged in)
const noRedirectRoutes = ["/logout"];
export default auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
@@ -28,11 +31,20 @@ export default auth((req) => {
nextUrl.pathname.startsWith("/assets") ||
nextUrl.pathname.includes(".");
const isNoRedirectRoute = noRedirectRoutes.some(
(route) => nextUrl.pathname === route || nextUrl.pathname.startsWith(route + "/")
);
// Skip middleware for API routes and static files
if (isApiRoute || isStaticRoute) {
return NextResponse.next();
}
// Never redirect away from no-redirect routes (e.g., /logout must always be accessible)
if (isNoRedirectRoute) {
return NextResponse.next();
}
// Allow access to public routes for everyone
if (isPublicRoute) {
return NextResponse.next();