-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
36 lines (31 loc) · 992 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
34
35
36
import { NextRequestWithAuth, withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"
async function middleware(req: NextRequestWithAuth) {
const notAdmin = req.nextauth.token?.role !== "teacher"
const isLogin = req.nextauth.token ? true : false
const dashboardRoutes =
req.nextUrl.pathname.startsWith("/dashboard") ||
req.nextUrl.pathname.startsWith("/dashboard/create-exam") ||
req.nextUrl.pathname.startsWith("/dashboard/user") ||
req.nextUrl.pathname.startsWith("/dashboard/user/create")
// check if user is not admin
if (dashboardRoutes && (isLogin || !isLogin) && notAdmin) {
return NextResponse.rewrite(new URL("/denied", req.url))
}
}
export default withAuth(middleware, {
callbacks: {
authorized: ({ token }) => !!token,
},
pages: {
signIn: "/sign-in",
},
})
export const config = {
matcher: [
"/dashboard",
"/dashboard/create-exam",
"/dashboard/user",
"/dashboard/user/create",
],
}