-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
28 lines (26 loc) · 917 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
import { withAuth } from 'next-auth/middleware'
import { NextResponse } from 'next/server'
export default withAuth(
function middleware(req: any) {
// this validation will cause that we won't be able to use the RequireFeature component. So instead of
// showing Access denied will perform the redirect. How to use that depends probably on the requirements
if (req.nextUrl.pathname.startsWith('/admin') && !req.nextauth.token.user.features.includes('admin')) {
return NextResponse.redirect(
new URL('/?message=NotAdmin', req.url)
)
}
if (req.nextUrl.pathname.startsWith('/user') && !req.nextauth.token.user.features.includes('user')) {
return NextResponse.redirect(
new URL('/?message=NotUser', req.url)
)
}
},
{
callbacks: {
authorized: ({ token }: any) => !!token
},
}
)
export const config = {
matcher: ['/admin', '/user']
}