-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
38 lines (30 loc) · 970 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
29
30
31
32
33
34
35
36
37
38
import { NextResponse, NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const isAuthenticated = true
const isAdmin = true
if (
request.nextUrl.pathname.startsWith('/admin') ||
// request.nextUrl.pathname.startsWith('/admin/user') ||
request.nextUrl.pathname.startsWith('/dashboard')
) {
if (isAuthenticated && isAdmin) {
return NextResponse.next()
}
else {
return NextResponse.redirect(new URL('/', request.url))
}
}
if ( request.nextUrl.pathname.startsWith('/user') ) {
if (isAuthenticated && isAdmin) {
return NextResponse.redirect(new URL('/admin/user', request.url))
}
else if (isAuthenticated) {
return NextResponse.next()
} else {
return NextResponse.redirect(new URL('/sign-up', request.url))
}
}
}
export const config = {
matcher: ['/user', '/admin', '/admin/user', '/dashboard:path*'],
}