aboutsummaryrefslogtreecommitdiff
path: root/communities/red-right-hand/174bg.net/src/proxy.js
blob: dc0fa42139e4bd7e69c48bb48aeca1fc1da2bc68 (plain)
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
import { NextResponse } from "next/server";

export async function proxy(request) {
  let session = null;

  try {
    const internalUrl =
      process.env.BETTER_AUTH_URL_INTERNAL || "http://localhost:3000";
    const res = await fetch(new URL("/api/auth/get-session", internalUrl), {
      headers: {
        cookie: request.headers.get("cookie") ?? "",
      },
    });
    session = await res.json();
  } catch {
    // If session check fails, treat as unauthenticated
  }

  if (!session?.user) {
    const loginUrl = new URL("/login", request.url);
    loginUrl.searchParams.set("callbackUrl", request.nextUrl.pathname);
    return NextResponse.redirect(loginUrl);
  }

  return NextResponse.next();
}

export const config = {
  matcher: ["/secure/:path*"],
};