Adding image link now works for pfp

This commit is contained in:
Gabriel Brown 2024-08-08 16:35:59 -05:00
parent 40130b65e5
commit 2989cdd421
5 changed files with 74 additions and 6 deletions

View File

@ -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 });
}
};

View File

@ -17,8 +17,8 @@ import Sign_Out_Button from "~/components/auth/server/SignOutButton"
export default async function Avatar_Popover() { export default async function Avatar_Popover() {
const session = await auth(); const session = await auth();
const pfp = session?.user?.image ?? "https://github.com/shadcn.png"; const pfp = session?.user?.image ?? "";
const users_name = session?.user?.name ?? "shadcn"; const users_name = session?.user?.name ?? "New User";
const initials = users_name.split(" ").map((name) => name[0]).join(""); const initials = users_name.split(" ").map((name) => name[0]).join("");
console.log(pfp); console.log(pfp);
return ( return (

View File

@ -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) => { const update_users_name = async (users_name: string, users_email: string) => {
try { try {
const res = await fetch("/api/users/set_username_by_email", { const res = await fetch("/api/users/set_users_name_by_email", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "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<typeof formSchema>) => { const onSubmit = async (data: z.infer<typeof formSchema>) => {
if (data.users_profile_image === undefined) {
data.users_profile_image = "";
}
await update_users_name(data.users_name, users_email); await update_users_name(data.users_name, users_email);
await update_users_pfp(data.users_profile_image, users_email);
setIsOpen(false); setIsOpen(false);
}; };

View File

@ -6,7 +6,6 @@ import { eq } from "drizzle-orm"
export const set_users_name_by_email = async (users_name: string, users_email: string) => { export const set_users_name_by_email = async (users_name: string, users_email: string) => {
try { try {
console.log('Updating user:', users_email, 'with name:', users_name); // Log input
await db.update(schema.users) await db.update(schema.users)
.set({ name: users_name }) .set({ name: users_name })
.where(eq(schema.users.email, users_email)) .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({ const result = await db.select({
users_name: schema.users.name, users_name: schema.users.name,
}).from(schema.users) }).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; return result;
} }