default layuout
This commit is contained in:
@@ -7,11 +7,15 @@ const publicRoutes = ["/login", "/signup", "/forgot-password", "/reset-password"
|
||||
export default auth((req) => {
|
||||
const { nextUrl } = req;
|
||||
const isLoggedIn = !!req.auth;
|
||||
const userRole = (req.auth?.user as any)?.role;
|
||||
|
||||
const isPublicRoute = publicRoutes.some(
|
||||
(route) => nextUrl.pathname === route || nextUrl.pathname.startsWith(route + "/")
|
||||
);
|
||||
|
||||
const isAgentRoute = nextUrl.pathname.startsWith("/agent");
|
||||
const isUserRoute = nextUrl.pathname.startsWith("/user");
|
||||
|
||||
const isApiRoute = nextUrl.pathname.startsWith("/api");
|
||||
const isStaticRoute = nextUrl.pathname.startsWith("/_next") ||
|
||||
nextUrl.pathname.startsWith("/assets") ||
|
||||
@@ -24,7 +28,11 @@ export default auth((req) => {
|
||||
|
||||
// Redirect logged-in users away from public routes (login, signup)
|
||||
if (isLoggedIn && isPublicRoute) {
|
||||
return NextResponse.redirect(new URL("/", nextUrl.origin));
|
||||
// Redirect to appropriate dashboard based on role
|
||||
if (userRole === "AGENT") {
|
||||
return NextResponse.redirect(new URL("/agent/dashboard", nextUrl.origin));
|
||||
}
|
||||
return NextResponse.redirect(new URL("/user/dashboard", nextUrl.origin));
|
||||
}
|
||||
|
||||
// Redirect non-logged-in users to login page for protected routes
|
||||
@@ -34,6 +42,19 @@ export default auth((req) => {
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
|
||||
// Role-based route protection
|
||||
if (isLoggedIn) {
|
||||
// Prevent regular users from accessing agent routes
|
||||
if (isAgentRoute && userRole !== "AGENT") {
|
||||
return NextResponse.redirect(new URL("/user/dashboard", nextUrl.origin));
|
||||
}
|
||||
|
||||
// Prevent agents from accessing user routes
|
||||
if (isUserRoute && userRole === "AGENT") {
|
||||
return NextResponse.redirect(new URL("/agent/dashboard", nextUrl.origin));
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user