a tool for shared writing and social publishing
1"use server";
2import { cookies } from "next/headers";
3import { Database } from "supabase/database.types";
4import { createServerClient } from "@supabase/ssr";
5import { Vercel } from "@vercel/sdk";
6
7let supabase = createServerClient<Database>(
8 process.env.NEXT_PUBLIC_SUPABASE_API_URL as string,
9 process.env.SUPABASE_SERVICE_ROLE_KEY as string,
10 { cookies: {} },
11);
12
13const VERCEL_TOKEN = process.env.VERCEL_TOKEN;
14const vercel = new Vercel({
15 bearerToken: VERCEL_TOKEN,
16});
17export async function deleteDomain({ domain }: { domain: string }) {
18 let auth_token = (await cookies()).get("auth_token")?.value;
19 if (!auth_token) return null;
20 let { data: auth_data } = await supabase
21 .from("email_auth_tokens")
22 .select(
23 `*,
24 identities(
25 *,
26 custom_domains!custom_domains_identity_fkey(*)
27 )`,
28 )
29 .eq("id", auth_token)
30 .eq("confirmed", true)
31 .single();
32 if (
33 !auth_data ||
34 !auth_data.email ||
35 !auth_data.identities?.custom_domains.find((d) => d.domain === domain)
36 )
37 return null;
38
39 await supabase.from("custom_domain_routes").delete().eq("domain", domain);
40 await supabase.from("custom_domains").delete().eq("domain", domain);
41 await vercel.projects.removeProjectDomain({
42 idOrName: "prj_9jX4tmYCISnm176frFxk07fF74kG",
43 teamId: "team_42xaJiZMTw9Sr7i0DcLTae9d",
44 domain,
45 });
46
47 return true;
48}