diff --git a/src/actions/update-profile.ts b/src/actions/update-profile.ts new file mode 100644 index 0000000..197467f --- /dev/null +++ b/src/actions/update-profile.ts @@ -0,0 +1,42 @@ +// src/actions/update-profile.ts +"use server"; + +import { headers } from "next/headers"; +import { revalidatePath } from "next/cache"; + +import { auth } from "@/lib/auth"; +import { db } from "@/db"; +import { users } from "@/db/schema"; +import { eq } from "drizzle-orm"; + +export async function updateProfile(formData: FormData) { + const session = await auth.api.getSession({ + headers: await headers(), + }); + + if (!session?.user) { + throw new Error("Not authenticated"); + } + + const userId = session.user.id as any; + + const name = formData.get("name"); + const image = formData.get("image"); + + if (typeof name !== "string" || name.trim().length < 2) { + throw new Error("Name must be at least 2 characters"); + } + + await db + .update(users) + .set({ + name: name.trim(), + image: + typeof image === "string" && image.trim().length > 0 + ? image.trim() + : null, + }) + .where(eq(users.id, userId)); + + revalidatePath("/profile"); +} diff --git a/src/app/profile/page.tsx b/src/app/profile/page.tsx new file mode 100644 index 0000000..e75d4e4 --- /dev/null +++ b/src/app/profile/page.tsx @@ -0,0 +1,55 @@ +// src/app/profile/page.tsx +import { redirect } from "next/navigation"; +import { headers } from "next/headers"; + +import { auth } from "@/lib/auth"; +import { db } from "@/db"; +import { users } from "@/db/schema"; +import { eq } from "drizzle-orm"; +import { ProfileForm } from "@/components/ProfileForm"; + +export default async function ProfilePage() { + const session = await auth.api.getSession({ + headers: await headers(), + }); + + if (!session?.user) { + redirect("/sign-in"); + } + + const userId = session.user.id as any; + + const [user] = await db + .select() + .from(users) + .where(eq(users.id, userId)) + .limit(1); + + if (!user) { + redirect("/"); + } + + const displayName = user.name ?? ""; + const email = user.email ?? ""; + const image = user.image ?? null; + + const initials = + displayName && displayName.trim().length > 0 + ? displayName + .split(" ") + .map((part: string) => part[0]) + .join("") + .toUpperCase() + : "U"; + + return ( +