2025-04-04 02:04:38 +00:00
|
|
|
"use server";
|
|
|
|
|
|
|
|
import bcrypt from "bcrypt";
|
|
|
|
import prisma from "@/lib/prisma";
|
|
|
|
import { signIn } from "@/lib/auth";
|
|
|
|
import { authSchema } from "@/lib/zod";
|
2025-04-15 16:40:36 +00:00
|
|
|
import { getTranslations } from "next-intl/server";
|
2025-04-04 02:04:38 +00:00
|
|
|
import { CredentialsSignInFormValues } from "@/components/credentials-sign-in-form";
|
|
|
|
import { CredentialsSignUpFormValues } from "@/components/credentials-sign-up-form";
|
|
|
|
|
|
|
|
const saltRounds = 10;
|
|
|
|
|
2025-04-14 13:27:06 +00:00
|
|
|
export async function signInWithCredentials(formData: CredentialsSignInFormValues, redirectTo?: string) {
|
2025-04-15 16:40:36 +00:00
|
|
|
const t = await getTranslations("signInWithCredentials");
|
|
|
|
|
2025-04-04 02:04:38 +00:00
|
|
|
try {
|
|
|
|
// Parse credentials using authSchema for validation
|
|
|
|
const { email, password } = await authSchema.parseAsync(formData);
|
|
|
|
|
|
|
|
// Find user by email
|
|
|
|
const user = await prisma.user.findUnique({ where: { email } });
|
|
|
|
|
|
|
|
// Check if the user exists
|
|
|
|
if (!user) {
|
2025-04-15 16:40:36 +00:00
|
|
|
throw new Error(t("userNotFound"));
|
2025-04-04 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user has a password
|
|
|
|
if (!user.password) {
|
2025-04-15 16:40:36 +00:00
|
|
|
throw new Error(t("invalidCredentials"));
|
2025-04-04 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the password matches
|
|
|
|
const passwordMatch = await bcrypt.compare(password, user.password);
|
|
|
|
if (!passwordMatch) {
|
2025-04-15 16:40:36 +00:00
|
|
|
throw new Error(t("incorrectPassword"));
|
2025-04-04 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
2025-04-14 13:27:06 +00:00
|
|
|
await signIn("credentials", { ...formData, redirectTo, redirect: !!redirectTo });
|
2025-04-04 02:04:38 +00:00
|
|
|
return { success: true };
|
|
|
|
} catch (error) {
|
2025-04-15 16:40:36 +00:00
|
|
|
return { error: error instanceof Error ? error.message : t("signInFailedFallback") };
|
2025-04-04 02:04:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function signUpWithCredentials(formData: CredentialsSignUpFormValues) {
|
2025-04-15 16:40:36 +00:00
|
|
|
const t = await getTranslations("signUpWithCredentials");
|
|
|
|
|
2025-04-04 02:04:38 +00:00
|
|
|
try {
|
|
|
|
const validatedData = await authSchema.parseAsync(formData);
|
|
|
|
|
|
|
|
// Check if user already exists
|
|
|
|
const existingUser = await prisma.user.findUnique({ where: { email: validatedData.email } });
|
|
|
|
if (existingUser) {
|
2025-04-15 16:40:36 +00:00
|
|
|
throw new Error(t("userAlreadyExists"));
|
2025-04-04 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash password and create user
|
|
|
|
const pwHash = await bcrypt.hash(validatedData.password, saltRounds);
|
|
|
|
const user = await prisma.user.create({
|
|
|
|
data: { email: validatedData.email, password: pwHash },
|
|
|
|
});
|
|
|
|
|
|
|
|
// Assign admin role if first user
|
|
|
|
const userCount = await prisma.user.count();
|
|
|
|
if (userCount === 1) {
|
|
|
|
await prisma.user.update({ where: { id: user.id }, data: { role: "ADMIN" } });
|
|
|
|
}
|
|
|
|
|
|
|
|
return { success: true };
|
|
|
|
} catch (error) {
|
2025-04-15 16:40:36 +00:00
|
|
|
return { error: error instanceof Error ? error.message : t("registrationFailedFallback") };
|
2025-04-04 02:04:38 +00:00
|
|
|
}
|
|
|
|
}
|
2025-04-14 13:27:06 +00:00
|
|
|
|
|
|
|
export async function signInWithGithub(redirectTo?: string) {
|
|
|
|
await signIn("github", { redirectTo, redirect: !!redirectTo });
|
|
|
|
}
|