-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
25 lines (18 loc) · 793 Bytes
/
middleware.ts
File metadata and controls
25 lines (18 loc) · 793 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const currentUser = request.cookies.get('currentUser')?.value
const isDashboardPage = request.nextUrl.pathname.startsWith('/dashboard')
if (isDashboardPage && !currentUser) {
return NextResponse.redirect(new URL('/sign-in', request.url))
}
const isAuthPage = request.nextUrl.pathname.startsWith('/sign-in') ||
request.nextUrl.pathname.startsWith('/sign-up')
if (isAuthPage && currentUser) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/dashboard/:path*', '/sign-in', '/sign-up'],
}