-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
45 lines (36 loc) · 1.16 KB
/
middleware.ts
File metadata and controls
45 lines (36 loc) · 1.16 KB
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getToken } from "next-auth/jwt"
const secret = process.env.NEXTAUTH_SECRET
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname
const token = await getToken({ req: request, secret: secret });
const isPublicPath = path.startsWith("/auth")
const isAdminPath = path === '/admin' || path.startsWith("/admin")
const isProPath = path === '/pro' || path.startsWith("/pro")
const isApprovalPath = path === '/approval' || path.startsWith("/approval")
if (isPublicPath && token) {
return NextResponse.redirect(new URL('/admin', request.nextUrl))
}
if (isAdminPath && !token) {
return NextResponse.redirect(new URL('/auth', request.nextUrl))
}
if (isProPath && !token) {
return NextResponse.redirect(new URL('/auth', request.nextUrl))
}
if (isApprovalPath && !token) {
return NextResponse.redirect(new URL('/auth', request.nextUrl))
}
}
export const config = {
matcher: [
'/',
'/auth',
'/admin',
'/admin/:path*',
'/pro',
'/pro/:path*',
'/approval',
'/approval/:path*',
]
}