2025-03-14 09:06:19 +00:00
|
|
|
"use server";
|
|
|
|
|
|
|
|
import bcrypt from "bcrypt";
|
|
|
|
import prisma from "@/lib/prisma";
|
|
|
|
import { signIn } from "@/lib/auth";
|
|
|
|
import { authSchema } from "@/lib/zod";
|
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
|
2025-03-14 09:15:38 +00:00
|
|
|
const saltRounds = 10;
|
|
|
|
|
2025-03-14 09:06:19 +00:00
|
|
|
export async function signInWithCredentials(formData: { email: string; password: string }) {
|
|
|
|
await signIn("credentials", formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function signUpWithCredentials(formData: { email: string; password: string }) {
|
|
|
|
const validatedData = await authSchema.parseAsync(formData);
|
2025-03-14 09:15:38 +00:00
|
|
|
|
2025-03-14 09:06:19 +00:00
|
|
|
const pwHash = await bcrypt.hash(validatedData.password, saltRounds);
|
|
|
|
|
2025-03-18 13:53:59 +00:00
|
|
|
const user = await prisma.user.create({
|
2025-03-14 09:06:19 +00:00
|
|
|
data: {
|
|
|
|
email: validatedData.email,
|
|
|
|
password: pwHash,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2025-03-18 13:53:59 +00:00
|
|
|
const count = await prisma.user.count();
|
|
|
|
if (count === 1) {
|
|
|
|
await prisma.user.update({
|
|
|
|
where: { id: user.id },
|
|
|
|
data: { role: "ADMIN" },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-14 09:06:19 +00:00
|
|
|
redirect("/sign-in");
|
|
|
|
}
|