-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
30 lines (27 loc) · 954 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
import { NextResponse } from "next/server";
import {
withMiddlewareAuthRequired,
getSession,
} from "@auth0/nextjs-auth0/edge";
export default withMiddlewareAuthRequired(async function middleware(req) {
const user = await getSession(req);
// Check for Admin privileges
if (!user.user["http://localhost:3000/roles"].includes("Admin")) {
// Redirect non-admin users to home page when requesting admin routes
if (req.nextUrl.pathname.startsWith("/admin")) {
const url = req.nextUrl.clone();
url.pathname = "/";
return NextResponse.redirect(url);
}
// Send forbidden status for non-admin api requests
if (req.nextUrl.pathname.startsWith("/api/admin")) {
return new Response("Unauthorized", { status: 401 });
}
}
// Continue if no filtering is done
return NextResponse.next();
});
// Only look at api and /Admin routes
export const config = {
matcher: ["/admin/:path*", "/api/:path*"],
};