-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
68 lines (59 loc) · 2.51 KB
/
Copy pathproxy.ts
File metadata and controls
68 lines (59 loc) · 2.51 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
import { NextResponse, type NextRequest } from "next/server";
import { createServerClient, type CookieOptions } from "@supabase/ssr";
import { trackReferralCode } from "@profullstack/stack/referrals";
type Cookie = { name: string; value: string; options?: CookieOptions };
export async function proxy(request: NextRequest) {
// 308 redirect www.crawlproof.com -> crawlproof.com (preserves method + body).
const host = request.headers.get("host") ?? "";
if (host.toLowerCase().startsWith("www.")) {
const target = request.nextUrl.clone();
target.host = host.slice(4);
target.protocol = "https";
target.port = "";
return NextResponse.redirect(target, 308);
}
let response = NextResponse.next({ request });
const supabase = createServerClient<any>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookies: Cookie[]) {
cookies.forEach(({ name, value }) => request.cookies.set(name, value));
response = NextResponse.next({ request });
cookies.forEach(({ name, value, options }) =>
response.cookies.set(name, value, options),
);
},
},
},
);
const { data, error } = await supabase.auth.getUser();
const user = error ? null : data.user;
const path = request.nextUrl.pathname;
// Every signed-in resource lives under /dashboard, so that one prefix is the
// whole gate. The old top-level paths (/projects, /audits, /settings, …) are
// 307'd here by next.config redirects, which run BEFORE middleware — so a
// signed-out visitor on an old bookmark lands on /dashboard/... and is
// caught by this check anyway, with the redirect param pointing at the new
// path rather than the dead one.
const isApp = path.startsWith("/dashboard");
if (isApp && !user) {
const url = request.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("redirect", path);
// 302 (Found) — temporary redirect: the resource exists but the user must
// authenticate first. Using an explicit status avoids relying on the
// Next.js default and keeps caches from storing the redirect permanently.
return NextResponse.redirect(url, 302);
}
return trackReferralCode(request, response as any);
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml|llms.txt|skill.md|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};