-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
28 lines (25 loc) · 871 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
// Ref: https://next-auth.js.org/configuration/nextjs#advanced-usage
import { NextResponse } from "next/server"
import { NextRequestWithAuth, withAuth } from "next-auth/middleware"
export default withAuth(
function middleware(request: NextRequestWithAuth) {
// console.log(request.nextUrl.pathname)
// console.log(request.nextauth.token)
if (
request.nextUrl.pathname.startsWith("/dashboard/pemilik") &&
request.nextauth.token?.role !== "Pemilik"
) {
return NextResponse.rewrite(new URL("/dashboard", request.url))
}
},
{
pages: {
signIn: "/login",
},
callbacks: {
authorized: ({ token }) => !!token,
},
},
)
// Ref: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
export const config = { matcher: ["/dashboard/:path*", "/dashboard/pemilik/:path*", "/login"] }