-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathproxy.ts
More file actions
32 lines (29 loc) · 1.01 KB
/
proxy.ts
File metadata and controls
32 lines (29 loc) · 1.01 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
import { NextRequest, NextResponse } from 'next/server'
import { getSessionCookie } from 'better-auth/cookies'
export async function proxy(request: NextRequest) {
const sessionCookie = getSessionCookie(request, {
cookiePrefix: 'pic-impact'
})
if (request.nextUrl.pathname.startsWith('/api/v1') && !sessionCookie) {
return Response.json(
{ success: false, message: 'authentication failed' },
{ status: 401 }
)
}
if (request.nextUrl.pathname.startsWith('/admin') && !sessionCookie) {
return NextResponse.redirect(new URL('/login', request.url))
}
if (sessionCookie && request.nextUrl.pathname === '/login') {
return NextResponse.redirect(new URL('/', request.url))
}
return NextResponse.next()
}
// Optionally, don't invoke Middleware on some paths
// Read more: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico).*)',
'/admin/:path*',
'/api/v1/:path*',
],
}