forked from AlwanN01/laundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.example.ts
33 lines (28 loc) · 1.15 KB
/
middleware.example.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
// 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(
// `withAuth` augments your `Request` with the user's token.
function middleware(request: NextRequestWithAuth) {
// console.log(request.nextUrl.pathname)
// console.log(request.nextauth.token)
if (request.nextUrl.pathname.startsWith("/extra") && request.nextauth.token?.role !== "admin") {
return NextResponse.rewrite(new URL("/denied", request.url))
}
if (
request.nextUrl.pathname.startsWith("/client") &&
request.nextauth.token?.role !== "admin" &&
request.nextauth.token?.role !== "manager"
) {
return NextResponse.rewrite(new URL("/denied", request.url))
}
},
{
callbacks: {
authorized: ({ token }) => !!token, //if true middleware will be executed
},
}
)
// Applies next-auth only to matching routes - can be regex
// Ref: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
export const config = { matcher: ["/extra", "/client", "/dashboard"] }