-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
34 lines (28 loc) · 936 Bytes
/
middleware.js
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
import { NextResponse } from "next/server";
import { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';
import { checkRole } from "@/lib/api-functions/server/utils";
import settings from "@/lib/api-functions/server/permissions";
const {
identifier,
roles: { admin: adminRole },
} = settings;
export const config = {
matcher: ["/admin/(.*)"],
};
export default withMiddlewareAuthRequired(async function middleware(req) {
try {
const res = NextResponse.next();
const {user} = await getSession(req, res);
// console.log("user", user);
const isAdmin = checkRole(user, identifier, adminRole);
// console.log("isAdmin", isAdmin);
if (!isAdmin) {
return NextResponse.redirect(new URL("/", req.url));
}
return res;
} catch (err) {
// console.log("in error", err);
// If not logged in
NextResponse.redirect(new URL("/api/auth/#", req.url));
}
});