From 2989cdd4211281d682bc7da129aa81b632c3671d Mon Sep 17 00:00:00 2001 From: gibbyb Date: Thu, 8 Aug 2024 16:35:59 -0500 Subject: [PATCH] Adding image link now works for pfp --- .../route.ts | 0 .../api/users/set_users_pfp_by_email/route.ts | 27 +++++++++++++++++++ src/components/auth/AvatarPopover.tsx | 4 +-- src/components/auth/FirstSignInForm.tsx | 25 ++++++++++++++++- src/server/functions.ts | 24 ++++++++++++++--- 5 files changed, 74 insertions(+), 6 deletions(-) rename src/app/api/users/{set_username_by_email => set_users_name_by_email}/route.ts (100%) create mode 100644 src/app/api/users/set_users_pfp_by_email/route.ts diff --git a/src/app/api/users/set_username_by_email/route.ts b/src/app/api/users/set_users_name_by_email/route.ts similarity index 100% rename from src/app/api/users/set_username_by_email/route.ts rename to src/app/api/users/set_users_name_by_email/route.ts diff --git a/src/app/api/users/set_users_pfp_by_email/route.ts b/src/app/api/users/set_users_pfp_by_email/route.ts new file mode 100644 index 0000000..2ad1f65 --- /dev/null +++ b/src/app/api/users/set_users_pfp_by_email/route.ts @@ -0,0 +1,27 @@ +"use server" +import { NextResponse } from "next/server" +import type { NextRequest } from "next/server" +import { set_users_pfp_by_email } from "~/server/functions" +import { auth } from "~/auth" + +type updateNameData = { + users_pfp: string; + users_email: string; +}; + +export const POST = async (req: NextRequest) => { + const session = await auth(); + if (!session) return NextResponse.json( + { error: "Not authenticated" }, + { status: 401 } + ); + const { users_pfp, users_email } = await req.json() as updateNameData; + console.log('API received users_pfp:', users_pfp, 'users_id:', users_email); // Log received data + try { + await set_users_pfp_by_email(users_pfp, users_email); + return NextResponse.json({ message: "Username updated successfully", users_pfp }, { status: 200 }); + } catch (error) { + console.error('Error in API route:', error); + return NextResponse.json({ error: "Error updating username" }, { status: 500 }); + } +}; diff --git a/src/components/auth/AvatarPopover.tsx b/src/components/auth/AvatarPopover.tsx index 93bdc16..d562bb1 100644 --- a/src/components/auth/AvatarPopover.tsx +++ b/src/components/auth/AvatarPopover.tsx @@ -17,8 +17,8 @@ import Sign_Out_Button from "~/components/auth/server/SignOutButton" export default async function Avatar_Popover() { const session = await auth(); - const pfp = session?.user?.image ?? "https://github.com/shadcn.png"; - const users_name = session?.user?.name ?? "shadcn"; + const pfp = session?.user?.image ?? ""; + const users_name = session?.user?.name ?? "New User"; const initials = users_name.split(" ").map((name) => name[0]).join(""); console.log(pfp); return ( diff --git a/src/components/auth/FirstSignInForm.tsx b/src/components/auth/FirstSignInForm.tsx index cf3b0ac..b92adf0 100644 --- a/src/components/auth/FirstSignInForm.tsx +++ b/src/components/auth/FirstSignInForm.tsx @@ -36,7 +36,7 @@ export default function First_Sign_In_Form({ users_name, users_email }: { users_ const update_users_name = async (users_name: string, users_email: string) => { try { - const res = await fetch("/api/users/set_username_by_email", { + const res = await fetch("/api/users/set_users_name_by_email", { method: "POST", headers: { "Content-Type": "application/json", @@ -54,9 +54,32 @@ export default function First_Sign_In_Form({ users_name, users_email }: { users_ } }; + const update_users_pfp = async (users_pfp: string, users_email: string) => { + try { + const res = await fetch("/api/users/set_users_pfp_by_email", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + users_pfp: users_pfp, + users_email: users_email, + }), + }); + if (!res.ok) { + throw new Error("Failed to update user's name"); + } + } catch (error) { + console.error("Could not update user's name", error); + } + }; const onSubmit = async (data: z.infer) => { + if (data.users_profile_image === undefined) { + data.users_profile_image = ""; + } await update_users_name(data.users_name, users_email); + await update_users_pfp(data.users_profile_image, users_email); setIsOpen(false); }; diff --git a/src/server/functions.ts b/src/server/functions.ts index ac97eb9..e179954 100644 --- a/src/server/functions.ts +++ b/src/server/functions.ts @@ -6,7 +6,6 @@ import { eq } from "drizzle-orm" export const set_users_name_by_email = async (users_name: string, users_email: string) => { try { - console.log('Updating user:', users_email, 'with name:', users_name); // Log input await db.update(schema.users) .set({ name: users_name }) .where(eq(schema.users.email, users_email)) @@ -16,10 +15,29 @@ export const set_users_name_by_email = async (users_name: string, users_email: s } }; -export const get_users_name_by_id = async (users_id: string) => { +export const get_users_name_by_email = async (users_email: string) => { const result = await db.select({ users_name: schema.users.name, }).from(schema.users) - .where(eq(schema.users.id, users_id)) + .where(eq(schema.users.email, users_email)) + return result; +} + +export const set_users_pfp_by_email = async (users_pfp: string, users_email: string) => { + try { + await db.update(schema.users) + .set({ image: users_pfp }) + .where(eq(schema.users.email, users_email)) + } catch (error) { + console.error('Error updating user pfp:', error); + throw error; + } +}; + +export const get_users_pfp_by_email = async (users_email: string) => { + const result = await db.select({ + users_pfp: schema.users.image, + }).from(schema.users) + .where(eq(schema.users.email, users_email)) return result; }