-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
49 lines (37 loc) · 1.29 KB
/
middleware.js
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
43
44
45
46
47
48
49
import { withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"
export default withAuth(
function middleware(request) {
// console.log(request.nextauth.token);
const USER = request.nextauth.token
const VISITOR = USER?.role === 'VISITOR'
const CONTRIBUTOR = USER?.role === 'CONTRIBUTOR'
const ADMIN = USER?.role === 'ADMIN'
if (request.nextUrl.pathname.startsWith('/doner/register') && (!USER)) {
return NextResponse.rewrite(new URL('/signup', request.url))
}
if (request.nextUrl.pathname.startsWith('/doner/approval') && (VISITOR)) {
return NextResponse.rewrite(new URL('/access-denide', request.url))
}
if (request.nextUrl.pathname.startsWith('/admin') && (!ADMIN)) {
return NextResponse.rewrite(new URL('/access-denide', request.url))
}
},
{
callbacks: {
authorized: async ({ req, token }) => {
if (!token) return false
}
},
secret: process.env.JWT_SECRET,
},
)
// See "Matching Paths" below to learn more
export const config = {
matcher: [
'/doner/register/:path*',
'/doner/approval/:path*',
'/UserProfile',
'/admin/:path*'
]
}