-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
42 lines (37 loc) · 1.09 KB
/
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
37
38
39
40
41
42
import { auth } from "@/auth"
export default auth(req => {
// Look for session required routes
if (
!req.auth &&
["/account"].some(route => req.nextUrl.pathname.includes(route))
) {
const newUrl = new URL(
`/sign-in?redirectTo=${req.nextUrl.pathname}`,
req.nextUrl.origin
)
return Response.redirect(newUrl)
}
// Look for admin session required routes
if (
req.auth?.user.role !== "admin" &&
["/dashboard"].some(route => req.nextUrl.pathname.includes(route))
) {
const newUrl = new URL(
`/sign-in?redirectTo=${req.nextUrl.pathname}`,
req.nextUrl.origin
)
return Response.redirect(newUrl)
}
})
export const config = {
matcher: ["/dashboard/:path*", "/account/:path*"],
}
// If you don't want to use the middleware to check for the role and the page they are on
// and you would rather handle that on a page level, you can use the following code
// and check for the role in the layout/page RSC.
/*
export { auth as middleware } from "@/auth"
export const config = {
matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"],
}
*/