-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
29 lines (24 loc) · 1.15 KB
/
Copy pathproxy.ts
File metadata and controls
29 lines (24 loc) · 1.15 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
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth/auth-options";
// Named `proxy` (not `middleware`) per the Next.js 16 rename -- see
// node_modules/next/dist/docs/01-app/03-api-reference/03-file-conventions/proxy.md
const PROTECTED_PAGE_PREFIXES = ["/dashboard"];
const PROTECTED_API_PREFIXES = ["/api/query", "/api/history", "/api/datasets"];
export const proxy = auth((req) => {
const { pathname } = req.nextUrl;
if (req.auth) return;
// API routes must return JSON, never an HTML redirect -- a 307 to /login
// would otherwise be the first thing any API client (or the smoke test)
// sees instead of a 401.
if (PROTECTED_API_PREFIXES.some((prefix) => pathname.startsWith(prefix))) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
if (PROTECTED_PAGE_PREFIXES.some((prefix) => pathname.startsWith(prefix))) {
const loginUrl = new URL("/login", req.nextUrl);
loginUrl.searchParams.set("callbackUrl", pathname);
return NextResponse.redirect(loginUrl);
}
});
export const config = {
matcher: ["/dashboard/:path*", "/api/query/:path*", "/api/history/:path*", "/api/datasets/:path*"],
};