45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import { createServerClient } from "@supabase/ssr";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { legacyRecoveryToken } from "./shared/legacy-recovery";
|
|
export async function proxy(request: NextRequest) {
|
|
const recoveryToken = legacyRecoveryToken(request.nextUrl.pathname);
|
|
if (recoveryToken) {
|
|
const destination = request.nextUrl.clone();
|
|
destination.pathname = "/auth/callback";
|
|
destination.search = "";
|
|
destination.searchParams.set("legacy_recovery_token", recoveryToken);
|
|
return NextResponse.redirect(destination);
|
|
}
|
|
let response = NextResponse.next({ request });
|
|
// Developer requests authenticate with their own opaque key, never browser cookies.
|
|
if (
|
|
request.nextUrl.pathname.startsWith("/api/dev/v1/") ||
|
|
request.nextUrl.pathname === "/api/dev/openapi.json"
|
|
)
|
|
return response;
|
|
if (!process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY) return response;
|
|
const client = createServerClient(
|
|
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
|
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY,
|
|
{
|
|
cookies: {
|
|
getAll: () => request.cookies.getAll(),
|
|
setAll: (values) => {
|
|
values.forEach(({ name, value }) => request.cookies.set(name, value));
|
|
response = NextResponse.next({ request });
|
|
values.forEach(({ name, value, options }) =>
|
|
response.cookies.set(name, value, options),
|
|
);
|
|
},
|
|
},
|
|
},
|
|
);
|
|
await client.auth.getUser();
|
|
return response;
|
|
}
|
|
export const config = {
|
|
matcher: [
|
|
"/((?!_next/static|_next/image|favicon.ico|\\.well-known/workflow/|tools/|desktop/|fonts/|images/|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|ttf|woff|woff2|jar)$).*)",
|
|
],
|
|
};
|