a tool for shared writing and social publishing
1"use server";
2
3import { cookies } from "next/headers";
4import { supabaseServerClient } from "supabase/serverClient";
5import { cache } from "react";
6export const getIdentityData = cache(uncachedGetIdentityData);
7export async function uncachedGetIdentityData() {
8 let cookieStore = await cookies();
9 let auth_token =
10 cookieStore.get("auth_token")?.value ||
11 cookieStore.get("external_auth_token")?.value;
12 let auth_res = auth_token
13 ? await supabaseServerClient
14 .from("email_auth_tokens")
15 .select(
16 `*,
17 identities(
18 *,
19 bsky_profiles(*),
20 publication_subscriptions(*),
21 custom_domains!custom_domains_identity_id_fkey(publication_domains(*), *),
22 home_leaflet:permission_tokens!identities_home_page_fkey(*, permission_token_rights(*,
23 entity_sets(entities(facts(*)))
24 )),
25 permission_token_on_homepage(
26 created_at,
27 permission_tokens!inner(
28 id,
29 root_entity,
30 permission_token_rights(*),
31 leaflets_in_publications(*, publications(*), documents(*))
32 )
33 )
34 )`,
35 )
36 .eq("id", auth_token)
37 .eq("confirmed", true)
38 .single()
39 : null;
40 if (!auth_res?.data?.identities) return null;
41 if (auth_res.data.identities.atp_did) {
42 //I should create a relationship table so I can do this in the above query
43 let { data: publications } = await supabaseServerClient
44 .from("publications")
45 .select("*")
46 .eq("identity_did", auth_res.data.identities.atp_did);
47 return {
48 ...auth_res.data.identities,
49 publications: publications || [],
50 };
51 }
52
53 return { ...auth_res.data.identities, publications: [] };
54}