-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (22 loc) · 768 Bytes
/
middleware.ts
File metadata and controls
27 lines (22 loc) · 768 Bytes
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { auth } from '@/auth'
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const session = await auth()
let currentUrl = request.nextUrl
if (session && currentUrl.pathname.startsWith('/login')) {
return NextResponse.redirect(new URL('/', request.url))
}
if (session && !session?.user.isAdmin && currentUrl.pathname.startsWith('/admin')) {
return NextResponse.redirect(new URL('/', request.url))
}
return NextResponse.next()
}
// See "Matching Paths" below to learn more
export const config = {
matcher: [
'/',
'/admin/:path*',
]
}