Files
wr.do/actions/update-user-name.ts
2024-10-29 18:23:14 +08:00

40 lines
814 B
TypeScript

"use server";
import { revalidatePath } from "next/cache";
import { auth } from "@/auth";
import { prisma } from "@/lib/db";
import { userNameSchema } from "@/lib/validations/user";
export type FormData = {
name: string;
};
export async function updateUserName(userId: string, data: FormData) {
try {
const session = await auth();
if (!session?.user || session?.user.id !== userId) {
throw new Error("Unauthorized");
}
const { name } = userNameSchema.parse(data);
// Update the user name.
await prisma.user.update({
where: {
id: userId,
},
data: {
name: name,
},
});
revalidatePath("/dashboard/settings");
return { status: "success" };
} catch (error) {
// console.log(error)
return { status: "error" };
}
}