-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
42 lines (33 loc) · 1.06 KB
/
proxy.ts
File metadata and controls
42 lines (33 loc) · 1.06 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
import { getSessionCookie } from "better-auth/cookies";
import { NextResponse, type NextRequest } from "next/server";
const AUTH_ROUTES = new Set(["/login", "/signup"]);
function buildLoginRedirect(request: NextRequest): URL {
const url = new URL("/login", request.url);
const next = `${request.nextUrl.pathname}${request.nextUrl.search}`;
url.searchParams.set("next", next);
return url;
}
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const hasSession = Boolean(getSessionCookie(request.headers));
if (AUTH_ROUTES.has(pathname)) {
return NextResponse.next();
}
if (!hasSession) {
return NextResponse.redirect(buildLoginRedirect(request));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/login",
"/signup",
"/dashboard/:path*",
"/rooms/:path*",
"/profile/:path*",
"/leaderboard/:path*",
"/settings/:path*",
"/room/:path*",
],
};
// — proxy.ts: Edge middleware — session cookie gate for dashboard/rooms/etc.; /login and /signup always pass through.