-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
27 lines (21 loc) · 730 Bytes
/
proxy.ts
File metadata and controls
27 lines (21 loc) · 730 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 { getToken } from 'next-auth/jwt'
export async function proxy(request: NextRequest) {
const token = await getToken({ req: request })
const pathname = request.nextUrl.pathname
const isPublicRoute =
pathname === '/' ||
pathname === '/login' ||
pathname.startsWith('/api/auth')
if (!token && !isPublicRoute) {
return NextResponse.redirect(new URL('/login', request.url))
}
if (token && pathname === '/login') {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
}