Skip to content

Commit

Permalink
Merge pull request #4 from zenzen-sol/sol/userid_patch
Browse files Browse the repository at this point in the history
fix: Move userId from client to server
  • Loading branch information
zenzen-sol authored Apr 16, 2024
2 parents e496aab + 829c32e commit 172496a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
17 changes: 2 additions & 15 deletions app/AddAccountForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const FormSchema = z.object({

const AddAccountForm: FC = () => {
const router = useRouter();
const { isLoaded, userId } = useAuth();
const { isLoaded } = useAuth();

const form = useForm<z.infer<typeof FormSchema>>({
mode: "onChange",
Expand All @@ -78,20 +78,7 @@ const AddAccountForm: FC = () => {

const onSubmit = async (values: z.infer<typeof FormSchema>) => {
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();
Expand Down
10 changes: 7 additions & 3 deletions app/actions/saveOrUpdateAccount.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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();
Expand Down

0 comments on commit 172496a

Please sign in to comment.