diff --git a/app/dashboard/actions.ts b/app/dashboard/actions.ts index 5ab2590..e37ef5f 100644 --- a/app/dashboard/actions.ts +++ b/app/dashboard/actions.ts @@ -74,17 +74,30 @@ export async function updateMinecraftName(form: FormData) { const mcName = (form.get("mcName") as string).trim(); if (!/^[A-Za-z0-9_]{3,16}$/.test(mcName)) throw new Error("invalid"); - await prisma.user.upsert({ + const user = await prisma.user.findUnique({ where: { email: session.user!.email! }, - update: { mcName }, - create: { - email: session.user!.email!, - name: session.user!.name, - image: session.user!.image, - mcName, - }, }); + if (!user) { + await prisma.user.create({ + data: { + email: session.user!.email!, + name: session.user!.name, + image: session.user!.image, + mcName, + }, + }); + } else { + if (user.mcName) { + throw new Error("Le pseudo Minecraft ne peut pas être modifié après avoir été défini."); + } + + await prisma.user.update({ + where: { email: session.user!.email! }, + data: { mcName }, + }); + } + revalidatePath("/dashboard"); revalidatePath("/"); -} \ No newline at end of file +} diff --git a/app/dashboard/profil/page.tsx b/app/dashboard/profil/page.tsx index 59747d9..5d34bb3 100644 --- a/app/dashboard/profil/page.tsx +++ b/app/dashboard/profil/page.tsx @@ -60,13 +60,15 @@ export default async function ProfilPage() { placeholder="Pseudo MC" required pattern="[A-Za-z0-9_]{3,16}" - className="input pl-12" + disabled={Boolean(user?.mcName)} + className="input pl-12 disabled:opacity-60" />