/users: kullanıcı listesi, ekleme formu, silme (kendi hesabını veya son kalan tek kullanıcıyı silmeye izin verilmiyor). Nav'a link eklendi. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
"use server";
|
||
|
||
import bcrypt from "bcryptjs";
|
||
import { prisma } from "@streamclipper/db";
|
||
import { revalidatePath } from "next/cache";
|
||
import { getSession } from "../../lib/auth";
|
||
|
||
export async function addUser(formData: FormData) {
|
||
const username = String(formData.get("username") ?? "").trim();
|
||
const password = String(formData.get("password") ?? "");
|
||
|
||
if (!username || password.length < 8) {
|
||
throw new Error("Kullanıcı adı gerekli, şifre en az 8 karakter olmalı.");
|
||
}
|
||
|
||
const passwordHash = await bcrypt.hash(password, 12);
|
||
await prisma.user.create({ data: { username, passwordHash } });
|
||
|
||
revalidatePath("/users");
|
||
}
|
||
|
||
export async function deleteUser(userId: string) {
|
||
const session = await getSession();
|
||
if (session?.sub === userId) {
|
||
throw new Error("Kendi hesabını silemezsin.");
|
||
}
|
||
|
||
const totalUsers = await prisma.user.count();
|
||
if (totalUsers <= 1) {
|
||
throw new Error("Son kalan kullanıcı silinemez.");
|
||
}
|
||
|
||
await prisma.user.delete({ where: { id: userId } });
|
||
revalidatePath("/users");
|
||
}
|