-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
48 lines (40 loc) · 1.19 KB
/
middleware.ts
File metadata and controls
48 lines (40 loc) · 1.19 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
42
43
44
45
46
47
48
import { withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"
export default withAuth(
function middleware(req) {
const token = req.nextauth.token
const { pathname } = req.nextUrl
// Super admin routes
if (pathname.startsWith("/super-admin")) {
if (token?.systemRole !== "SUPER_ADMIN") {
return NextResponse.redirect(new URL("/unauthorized", req.url))
}
}
// College admin routes
if (pathname.startsWith("/admin")) {
if (!token?.systemRole || token.systemRole === "PERSONNEL") {
return NextResponse.redirect(new URL("/unauthorized", req.url))
}
}
return NextResponse.next()
},
{
callbacks: {
authorized: ({ token, req }) => {
const { pathname } = req.nextUrl
// Allow access to auth pages
if (pathname.startsWith("/auth")) {
return true
}
// Require authentication for protected routes
if (pathname.startsWith("/admin") || pathname.startsWith("/super-admin")) {
return !!token
}
return true
},
},
},
)
export const config = {
matcher: ["/admin/:path*", "/super-admin/:path*", "/auth/:path*"],
}