-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
44 lines (35 loc) · 1.3 KB
/
proxy.ts
File metadata and controls
44 lines (35 loc) · 1.3 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { auth } from './lib/auth'
import { headers } from 'next/headers'
// This function can be marked `async` if using `await` inside
export async function proxy(request: NextRequest) {
try {
const session = await auth.api.getSession({ headers: await headers() })
const isAuthenticated = session?.user ? true : false
const isAuthPath = request.nextUrl.pathname.startsWith('/login') || request.nextUrl.pathname.startsWith('/signup')
const isProtectedPath = request.nextUrl.pathname == '/home'
if (isAuthPath && isAuthenticated) {
return NextResponse.redirect(new URL('/home', request.url))
}
if (!isProtectedPath) {
return NextResponse.next()
}
if (isProtectedPath && !isAuthenticated) {
return NextResponse.redirect(new URL('/login', request.url))
}
return NextResponse.next()
} catch (error) {
console.error('Proxy error:', error)
// Allow request to continue on error to prevent complete failure
return NextResponse.next()
}
}
// See "Matching Paths" below to learn more
export const config = {
matcher: [
'/home',
'/login',
'/signup',
]
}