-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
35 lines (33 loc) · 1.04 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
import { withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"
export default withAuth(
function middleware(request) {
const isAdmin = request.nextauth?.token?.email
const pathname = request.nextUrl?.pathname
if (pathname.startsWith("/manage")) {
if (pathname === "/manage/register" || pathname === "/manage/login") {
return NextResponse.next()
}
if (isAdmin) {
if (!pathname.startsWith("/manage/user")) {
const adminUserUrl = new URL("/manage/user", request.nextUrl.origin)
return NextResponse.redirect(adminUserUrl.toString())
}
return NextResponse.next()
} else {
if (!pathname.startsWith("/manage/login")) {
const adminLoginUrl = new URL("/manage/login", request.nextUrl.origin)
return NextResponse.redirect(adminLoginUrl.toString())
}
return NextResponse.next()
}
} else {
return NextResponse.next()
}
},
{
callbacks: {
authorized: async () => true,
},
},
)