sarransh's picture
Add Setup Page for User Creation
29fac62 verified
Raw History Blame Contribute Delete
1.41 kB
"use server";
import bcrypt from "bcryptjs";
import { prisma } from "@/lib/prisma";
export async function createAdminUser(email: string, password: string) {
if (!email || !password) {
return { success: false, error: "Missing email or password" };
}
try {
console.log(`[SETUP] Creating user: ${email}`);
// 1. Check if user exists
const existing = await prisma.user.findFirst({
where: { email }
});
if (existing) {
console.log(`[SETUP] User existing: ${existing.id}`);
return { success: false, error: `User with email ${email} already exists` };
}
// 2. Hash password
const passwordHash = await bcrypt.hash(password, 10);
console.log(`[SETUP] Hashed password: ${passwordHash.substring(0, 10)}...`);
// 3. Create user
const newUser = await prisma.user.create({
data: {
name: "Admin User",
email,
passwordHash,
role: "ADMIN"
}
});
console.log(`[SETUP] Success! User ID: ${newUser.id}`);
return { success: true };
} catch (error: any) {
console.error("[SETUP] Error:", error);
return {
success: false,
error: error.message || String(error)
};
}
}