forked from Codehagen/Proppi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
29 lines (25 loc) · 944 Bytes
/
proxy.ts
File metadata and controls
29 lines (25 loc) · 944 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
28
29
import { getSessionCookie } from 'better-auth/cookies'
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
/**
* Next.js Proxy (formerly Middleware).
*
* Important: keep this as an *optimistic* check only.
* We only check for presence of Better Auth session cookie and redirect if absent.
*/
export function proxy(request: NextRequest) {
// Optimistic cookie-only check (NOT full validation).
// This is the recommended approach for Next.js Proxy to avoid blocking requests.
const sessionCookie = getSessionCookie(request)
if (!sessionCookie) {
const url = request.nextUrl.clone()
url.pathname = '/sign-in'
// Keep it simple; the sign-in page can decide whether to use this param.
url.searchParams.set('redirect', request.nextUrl.pathname)
return NextResponse.redirect(url)
}
return NextResponse.next()
}
export const config = {
matcher: ['/dashboard/:path*'],
}