Add manual user creation in admin panel

- Server action createUser() with bcrypt password hash + Account record
- Form with name, email, password (show/hide + generate), role, emailVerified toggle
- Optional welcome email toggle (bypasses auto-hook for admin-created users)
- /admin/users/new page with breadcrumb navigation
- After creation, redirects to the new user's profile page
- "Добавить пользователя" button on the users list page

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-08 12:36:52 +05:00
parent 58a61d6f04
commit 99c143d670
4 changed files with 322 additions and 3 deletions
+54
View File
@@ -0,0 +1,54 @@
"use server";
import { headers } from "next/headers";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import bcrypt from "bcryptjs";
import { sendWelcomeEmail } from "@/lib/email";
export async function createUser(data: {
name: string;
email: string;
password: string;
role: string;
emailVerified: boolean;
sendWelcome: boolean;
}): Promise<{ success: true; userId: string } | { success: false; error: string }> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session || session.user.role !== "admin") {
return { success: false, error: "Нет доступа" };
}
const { name, email, password, role, emailVerified, sendWelcome } = data;
if (!name.trim() || !email.trim() || !password.trim()) {
return { success: false, error: "Заполните все обязательные поля" };
}
const existing = await prisma.user.findUnique({ where: { email } });
if (existing) {
return { success: false, error: "Пользователь с таким email уже существует" };
}
const hashedPassword = await bcrypt.hash(password, 10);
const user = await prisma.user.create({
data: { name: name.trim(), email: email.trim().toLowerCase(), role, emailVerified },
});
// Create credential account (Better Auth's internal structure)
await prisma.account.create({
data: {
userId: user.id,
accountId: user.id,
providerId: "credential",
password: hashedPassword,
},
});
if (sendWelcome) {
await sendWelcomeEmail(user.email, user.name).catch(() => {});
}
return { success: true, userId: user.id };
}
+29
View File
@@ -0,0 +1,29 @@
import Link from "next/link";
import { CreateUserForm } from "@/components/admin/create-user-form";
export const metadata = { title: "Новый пользователь" };
export default function NewUserPage() {
return (
<div className="p-8">
<nav className="text-xs mb-6 uppercase tracking-widest" style={{ color: "var(--muted-foreground)" }}>
<Link href="/admin/users" className="hover:underline">Пользователи</Link>
<span className="mx-2">/</span>
<span style={{ color: "var(--foreground)" }}>Новый пользователь</span>
</nav>
<div className="mb-6">
<h1
className="text-xs font-bold uppercase tracking-widest"
style={{ color: "var(--muted-foreground)" }}
>
Создание пользователя
</h1>
</div>
<div className="card-aubade p-6">
<CreateUserForm />
</div>
</div>
);
}
+13 -3
View File
@@ -1,6 +1,7 @@
import { prisma } from "@/lib/prisma";
import { Badge } from "@/components/ui/badge";
import Link from "next/link";
import { UserPlus } from "lucide-react";
const roleLabel: Record<string, string> = {
admin: "Администратор",
@@ -22,9 +23,18 @@ export default async function UsersPage() {
return (
<div className="p-8">
<div className="mb-6">
<h1 className="text-2xl font-semibold text-slate-800">Пользователи</h1>
<p className="text-slate-500 text-sm mt-0.5">{users.length} пользователей</p>
<div className="mb-6 flex items-center justify-between">
<div>
<h1 className="text-2xl font-semibold text-slate-800">Пользователи</h1>
<p className="text-slate-500 text-sm mt-0.5">{users.length} пользователей</p>
</div>
<Link
href="/admin/users/new"
className="btn-aubade btn-aubade-accent flex items-center gap-1.5 px-4 py-2 text-sm"
>
<UserPlus size={14} />
Добавить пользователя
</Link>
</div>
<div className="bg-white border border-slate-200 rounded-2xl overflow-hidden">