-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
111 lines (86 loc) · 3.03 KB
/
Copy pathproxy.ts
File metadata and controls
111 lines (86 loc) · 3.03 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { NextResponse, type NextRequest } from "next/server";
import { auth } from "@/lib/auth";
import { isMarketingDeployment } from "@/lib/deployment-mode";
const BYPASS_PREFIXES = ["/_next", "/api"];
const BYPASS_EXACT = new Set(["/favicon.ico", "/icon.svg", "/robots.txt", "/sitemap.xml"]);
const STATIC_FILE_PATTERN = /\.(?:svg|png|jpg|jpeg|gif|webp|ico)$/;
const PROTECTED_ROUTES = ["/dashboard", "/snippets", "/settings", "/auth/cli"];
export function shouldBypassProxy(pathname: string) {
if (BYPASS_EXACT.has(pathname)) {
return true;
}
if (BYPASS_PREFIXES.some((prefix) => pathname.startsWith(prefix))) {
return true;
}
return STATIC_FILE_PATTERN.test(pathname);
}
function isProtectedRoute(pathname: string) {
return PROTECTED_ROUTES.some((route) => pathname.startsWith(route));
}
function handleProtectedRoute(request: NextRequest, pathname: string, isAuthenticated: boolean) {
const callbackPath = `${pathname}${request.nextUrl.search}`;
if (isProtectedRoute(pathname) && !isAuthenticated) {
const redirectUrl = new URL("/sign-in", request.url);
redirectUrl.searchParams.set("callbackUrl", callbackPath);
return NextResponse.redirect(redirectUrl);
}
if (isAuthenticated && pathname === "/sign-in") {
const callbackUrl = request.nextUrl.searchParams.get("callbackUrl");
if (callbackUrl?.startsWith("/")) {
return NextResponse.redirect(new URL(callbackUrl, request.url));
}
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return null;
}
export function shouldAllowMarketingRoute(pathname: string) {
return (
pathname === "/" ||
pathname === "/api/health" ||
pathname.startsWith("/_next/") ||
(pathname === "/docs" || pathname.startsWith("/docs/")) ||
BYPASS_EXACT.has(pathname) ||
STATIC_FILE_PATTERN.test(pathname)
);
}
function handleMarketingRoute(request: NextRequest, pathname: string) {
if (shouldAllowMarketingRoute(pathname)) {
return null;
}
if (pathname.startsWith("/api/")) {
return NextResponse.json(
{
ok: false,
error: {
code: "not_available",
message: "The hosted site contains product documentation only. Run Cache locally to use the API.",
},
},
{ status: 404 }
);
}
return NextResponse.redirect(new URL("/docs/getting-started/installation", request.url));
}
export async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (isMarketingDeployment()) {
const marketingResponse = handleMarketingRoute(request, pathname);
if (marketingResponse) {
return marketingResponse;
}
return NextResponse.next();
}
if (shouldBypassProxy(pathname)) {
return NextResponse.next();
}
const session = await auth();
const isAuthenticated = Boolean(session?.user?.id);
const redirectResponse = handleProtectedRoute(request, pathname, isAuthenticated);
if (redirectResponse) {
return redirectResponse;
}
return NextResponse.next();
}
export const config = {
matcher: ["/:path*"],
};