-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathproxy.ts
More file actions
89 lines (76 loc) · 2.12 KB
/
Copy pathproxy.ts
File metadata and controls
89 lines (76 loc) · 2.12 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
import { NextResponse, type NextRequest } from "next/server";
import { createSupabaseServerClient } from "@/lib/supabase-server";
import { getSupabaseConfig } from "@/lib/supabase-config";
function redirectTo(
request: NextRequest,
pathname: string,
searchParams?: Record<string, string>,
) {
const url = request.nextUrl.clone();
url.pathname = pathname;
url.search = "";
if (searchParams) {
for (const [k, v] of Object.entries(searchParams))
url.searchParams.set(k, v);
}
return NextResponse.redirect(url);
}
export async function proxy(request: NextRequest) {
const { pathname, search } = request.nextUrl;
const searchParams = request.nextUrl.searchParams;
const isDashboardRoute =
pathname === "/dashboard" || pathname.startsWith("/dashboard/");
const publicAuthRoutes = [
"/",
"/auth/login",
"/auth/signup",
"/auth/forgot-password",
"/auth/update-password",
];
const isPublicAuthRoute = publicAuthRoutes.includes(pathname);
const hasRecoveryParams =
searchParams.get("type") === "recovery" ||
searchParams.has("code") ||
searchParams.has("access_token") ||
searchParams.has("refresh_token") ||
search.includes("code=") ||
search.includes("type=recovery") ||
search.includes("access_token=") ||
search.includes("refresh_token=");
let response = NextResponse.next({
request: {
headers: request.headers,
},
});
if (pathname.startsWith("/auth/update-password")) {
return response;
}
if (isPublicAuthRoute || hasRecoveryParams) {
return response;
}
const { supabaseUrl, supabaseAnonKey } = getSupabaseConfig();
const supabase = createSupabaseServerClient(
request,
response,
supabaseUrl,
supabaseAnonKey
);
const {
data: { user },
} = await supabase.auth.getUser();
if (isDashboardRoute && !user) {
const next = `${pathname}${search ?? ""}`;
return redirectTo(request, "/auth/login", { next });
}
return response;
}
export const config = {
matcher: [
"/",
"/dashboard/:path*",
"/auth/login",
"/auth/signup",
"/auth/forgot-password",
"/auth/update-password",
],
};