mirror of
https://github.com/massbug/judge4c.git
synced 2025-05-18 07:16:34 +00:00
feat(auth): enhance signIn and signUp with error handling and form value types
This commit is contained in:
parent
c9c07664cf
commit
bef8dcee44
@ -4,33 +4,65 @@ import bcrypt from "bcrypt";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { signIn } from "@/lib/auth";
|
||||
import { authSchema } from "@/lib/zod";
|
||||
import { redirect } from "next/navigation";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { CredentialsSignInFormValues } from "@/components/credentials-sign-in-form";
|
||||
import { CredentialsSignUpFormValues } from "@/components/credentials-sign-up-form";
|
||||
|
||||
const saltRounds = 10;
|
||||
|
||||
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);
|
||||
|
||||
const pwHash = await bcrypt.hash(validatedData.password, saltRounds);
|
||||
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
email: validatedData.email,
|
||||
password: pwHash,
|
||||
},
|
||||
});
|
||||
|
||||
const count = await prisma.user.count();
|
||||
if (count === 1) {
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { role: "ADMIN" },
|
||||
export async function signInWithCredentials(formData: CredentialsSignInFormValues) {
|
||||
try {
|
||||
await signIn("credentials", {
|
||||
...formData,
|
||||
redirect: false,
|
||||
});
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
return { error: error.message };
|
||||
}
|
||||
return { error: "Failed to sign in. Please try again." };
|
||||
}
|
||||
}
|
||||
|
||||
export async function signUpWithCredentials(formData: CredentialsSignUpFormValues) {
|
||||
try {
|
||||
const validatedData = await authSchema.parseAsync(formData);
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: validatedData.email },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
throw new Error("User already exists");
|
||||
}
|
||||
|
||||
const pwHash = await bcrypt.hash(validatedData.password, saltRounds);
|
||||
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
email: validatedData.email,
|
||||
password: pwHash,
|
||||
},
|
||||
});
|
||||
|
||||
const count = await prisma.user.count();
|
||||
if (count === 1) {
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { role: "ADMIN" },
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (error.code === "P2002") {
|
||||
return { error: "Email already registered" };
|
||||
}
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return { error: error.message };
|
||||
}
|
||||
return { error: "Registration failed. Please try again." };
|
||||
}
|
||||
|
||||
redirect("/sign-in");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user