-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathmiddleware.ts
More file actions
28 lines (26 loc) · 686 Bytes
/
middleware.ts
File metadata and controls
28 lines (26 loc) · 686 Bytes
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
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
export default withAuth(
function middleware(req) {
// Check for admin routes
if (req.nextUrl.pathname.startsWith("/admin")) {
if (req.nextauth.token?.role !== "admin") {
return NextResponse.redirect(new URL("/", req.url));
}
}
},
{
callbacks: {
authorized: ({ token, req }) => {
// Admin routes require admin token
if (req.nextUrl.pathname.startsWith("/admin")) {
return !!token && token.role === "admin";
}
return true;
},
},
}
);
export const config = {
matcher: ["/admin/:path*"]
};