feat: Centralize authentication redirects to middleware, remove client-side redirect logic, and refine logout process.
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user