-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
33 lines (28 loc) · 911 Bytes
/
middleware.ts
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
import { withAuth } from "next-auth/middleware";
import { NextRequest, NextResponse } from "next/server";
const isAdminRoute = (pathname: string) => {
return pathname.startsWith("/admin");
};
const isDashboardRoute = (pathname: string) => {
return pathname.startsWith("/dashboard");
};
export default withAuth(
function middleware(req) {
const { pathname } = req.nextUrl;
const token = req.nextauth.token;
//console.log("token in middleware",token)
if (isAdminRoute(pathname)) {
return NextResponse.rewrite(new URL(pathname, req.url));
}
return NextResponse.next();
},
{
callbacks: {
authorized({ token }) {
//console.log("token value in middleware callback", JSON.stringify(token))
return token?.role === "admin" || token?.role === "agent";
},
},
}
);
export const config = { matcher: ["/admin/:path*", "/dashboard/:path*"] };