From aab5add9bc5f26d21bbb5a9a23415c05e0ceaaee Mon Sep 17 00:00:00 2001 From: Sol Irvine Date: Tue, 16 Apr 2024 14:54:31 +0900 Subject: [PATCH 1/2] fix: Move userId from client to server --- app/actions/saveOrUpdateAccount.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/actions/saveOrUpdateAccount.ts b/app/actions/saveOrUpdateAccount.ts index 4c2a339..e9d63e2 100644 --- a/app/actions/saveOrUpdateAccount.ts +++ b/app/actions/saveOrUpdateAccount.ts @@ -1,15 +1,19 @@ "use server"; import { database } from "@/lib/database"; +import { auth } from "@clerk/nextjs"; import { accounts, accountsToChains, chains } from "db/schema"; import { eq } from "drizzle-orm"; export const saveOrUpdateAccount = async (values: { address: string; name: string; - userId: string; }) => { const db = await database(); + const { userId } = auth(); + if (!userId) { + throw new Error("User not found."); + } const _chains = await db .select() .from(chains) @@ -23,14 +27,14 @@ export const saveOrUpdateAccount = async (values: { .values({ name: values.name, address: values.address, - userId: values.userId, + userId: userId, }) .onConflictDoUpdate({ target: accounts.address, set: { address: values.address, name: values.name, - userId: values.userId, + userId: userId, }, }) .returning(); From 829c32e2b4f230d829023ea523d51df141b97fc1 Mon Sep 17 00:00:00 2001 From: Sol Irvine Date: Tue, 16 Apr 2024 14:56:57 +0900 Subject: [PATCH 2/2] Update AddAccountForm.tsx --- app/AddAccountForm.tsx | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/app/AddAccountForm.tsx b/app/AddAccountForm.tsx index 7bc1b3d..ef4c2c8 100644 --- a/app/AddAccountForm.tsx +++ b/app/AddAccountForm.tsx @@ -65,7 +65,7 @@ const FormSchema = z.object({ const AddAccountForm: FC = () => { const router = useRouter(); - const { isLoaded, userId } = useAuth(); + const { isLoaded } = useAuth(); const form = useForm>({ mode: "onChange", @@ -78,20 +78,7 @@ const AddAccountForm: FC = () => { const onSubmit = async (values: z.infer) => { try { - if (!userId) { - throw new Error("User not found."); - } - - const valuesWithUserId: { - address: string; - name: string; - userId: string; - } = { - ...values, - userId: userId, - }; - - saveOrUpdateAccount(valuesWithUserId); + saveOrUpdateAccount(values); toast.success("Address has been created"); form.reset(); router.refresh();