-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (33 loc) · 1.25 KB
/
middleware.ts
File metadata and controls
41 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { auth } from "./src/lib/auth";
export default auth((req) => {
const isLoggedIn = !!req.auth;
const { nextUrl } = req;
const isAuthRoute = nextUrl.pathname.startsWith("/login");
const isDashboardRoute = nextUrl.pathname.startsWith("/dashboard");
const isSuperAdminRoute = nextUrl.pathname.startsWith("/super-admin");
// Allow open routes (public)
if (!isDashboardRoute && !isSuperAdminRoute && !isAuthRoute) return;
// If on login page and logged in, redirect to appropriate dashboard
if (isAuthRoute) {
if (isLoggedIn) {
const role = req.auth?.user?.role;
return Response.redirect(new URL(role === "SUPER_ADMIN" ? "/super-admin" : "/dashboard", nextUrl));
}
return;
}
// If not logged in and trying to access protected routes
if (!isLoggedIn) {
return Response.redirect(new URL("/login", nextUrl));
}
// Role-based protection
const userRole = req.auth?.user?.role;
if (isSuperAdminRoute && userRole !== "SUPER_ADMIN") {
return Response.redirect(new URL("/dashboard", nextUrl));
}
if (isDashboardRoute && userRole === "SUPER_ADMIN") {
return Response.redirect(new URL("/super-admin", nextUrl));
}
});
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};