Files
screenclipper/apps/frontend/app/users/actions.ts
T
ayrisdevandClaude Sonnet 5 5fef7460cf frontend: kullanıcı yönetimi sayfası
/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>
2026-09-01 12:28:19 +03:00

36 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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");
}