Skip to content

Commit 082d164

Browse files
feat: migrate account bootstrap to dedicated api (#1648)
* feat: migrate account bootstrap to dedicated api * chore: remove chat helper test from account migration
1 parent 5c122a0 commit 082d164

File tree

3 files changed

+55
-150
lines changed

3 files changed

+55
-150
lines changed

app/api/account/route.tsx

Lines changed: 0 additions & 122 deletions
This file was deleted.

hooks/useUser.tsx

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { uploadFile } from "@/lib/arweave/uploadFile";
77
import { useAccount } from "wagmi";
88
import { toast } from "sonner";
99
import { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails";
10+
import { fetchOrCreateAccount } from "@/lib/accounts/fetchOrCreateAccount";
1011

1112
const useUser = () => {
12-
const { login, user, logout } = usePrivy();
13+
const { login, user, logout, getAccessToken } = usePrivy();
1314
const { address: wagmiAddress } = useAccount();
1415
const address = (user?.wallet?.address as Address) || wagmiAddress;
1516
const email = user?.email?.address;
@@ -119,37 +120,25 @@ const useUser = () => {
119120

120121
useEffect(() => {
121122
const init = async () => {
122-
const config = {
123-
method: "POST",
124-
body: JSON.stringify({
125-
email,
126-
wallet: address,
127-
}),
128-
headers: {
129-
"Content-Type": "application/json",
130-
},
131-
};
132-
const response = await fetch("/api/account", config);
133-
134-
if (!response.ok) {
135-
throw new Error(
136-
`Email API request failed with status: ${response.status}`
137-
);
138-
}
123+
const accessToken = await getAccessToken();
124+
const data = await fetchOrCreateAccount({
125+
email,
126+
wallet: address,
127+
accessToken,
128+
});
139129

140-
const data = await response.json();
141-
setUserData(data.data);
142-
setImage(data.data?.image || "");
143-
setInstruction(data.data?.instruction || "");
144-
setName(data?.data?.name || "");
145-
setOrganization(data?.data?.organization || "");
146-
setJobTitle(data?.data?.job_title || "");
147-
setRoleType(data?.data?.role_type || "");
148-
setCompanyName(data?.data?.company_name || "");
130+
setUserData(data);
131+
setImage(data.image || "");
132+
setInstruction(data.instruction || "");
133+
setName(data.name || "");
134+
setOrganization(data.organization || "");
135+
setJobTitle(data.job_title || "");
136+
setRoleType(data.role_type || "");
137+
setCompanyName(data.company_name || "");
149138
};
150139
if (!email && !address) return;
151140
init();
152-
}, [email, address]);
141+
}, [email, address, getAccessToken]);
153142

154143
return {
155144
address,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl";
2+
import type { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails";
3+
4+
interface FetchOrCreateAccountResponse {
5+
data: AccountWithDetails;
6+
}
7+
8+
/**
9+
* Creates or retrieves the current account from the dedicated API.
10+
*/
11+
export async function fetchOrCreateAccount({
12+
email,
13+
wallet,
14+
accessToken,
15+
}: {
16+
email?: string;
17+
wallet?: string;
18+
accessToken?: string | null;
19+
}): Promise<AccountWithDetails> {
20+
const response = await fetch(`${getClientApiBaseUrl()}/api/accounts`, {
21+
method: "POST",
22+
headers: {
23+
"Content-Type": "application/json",
24+
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
25+
},
26+
body: JSON.stringify({
27+
email,
28+
wallet,
29+
}),
30+
});
31+
32+
if (!response.ok) {
33+
throw new Error(`Account API request failed with status: ${response.status}`);
34+
}
35+
36+
const data: FetchOrCreateAccountResponse = await response.json();
37+
return data.data;
38+
}

0 commit comments

Comments
 (0)