diff --git a/app/admin/components/Sidebar.tsx b/app/admin/components/Sidebar.tsx index 403652f..8b1647a 100644 --- a/app/admin/components/Sidebar.tsx +++ b/app/admin/components/Sidebar.tsx @@ -2,7 +2,7 @@ import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; -import { LayoutDashboard, Image as ImageIcon, Briefcase, Mail, LogOut, Settings } from 'lucide-react'; +import { LayoutDashboard, Image as ImageIcon, Briefcase, Mail, LogOut, Settings, Users } from 'lucide-react'; import { logout } from '../actions'; const navItems = [ @@ -12,6 +12,7 @@ const navItems = [ { name: 'Hizmetler', href: '/admin/services', icon: Briefcase }, { name: 'İletişim Mesajları', href: '/admin/contact', icon: Mail }, { name: 'Site Ayarları', href: '/admin/settings', icon: Settings }, + { name: 'Kullanıcılar', href: '/admin/users', icon: Users }, ]; export function Sidebar() { diff --git a/app/admin/users/actions.ts b/app/admin/users/actions.ts new file mode 100644 index 0000000..1dfec7c --- /dev/null +++ b/app/admin/users/actions.ts @@ -0,0 +1,68 @@ +'use server'; + +import { prisma } from '@/lib/db'; +import { getSession } from '@/lib/auth'; +import { redirect } from 'next/navigation'; +import { revalidatePath } from 'next/cache'; +import bcrypt from 'bcryptjs'; + +async function requireAuth() { + const session = await getSession(); + if (!session) redirect('/admin/login'); +} + +export async function createUser(formData: FormData) { + await requireAuth(); + + const username = formData.get('username') as string; + const password = formData.get('password') as string; + const confirmPassword = formData.get('confirmPassword') as string; + + if (!username || !password) return; + if (password !== confirmPassword) return; + if (password.length < 6) return; + + const exists = await prisma.user.findUnique({ where: { username } }); + if (exists) return; + + const hashed = await bcrypt.hash(password, 10); + await prisma.user.create({ + data: { username, password: hashed }, + }); + + revalidatePath('/admin/users'); +} + +export async function deleteUser(formData: FormData) { + await requireAuth(); + + const id = formData.get('id') as string; + if (!id) return; + + // En az 1 kullanıcı kalsın + const count = await prisma.user.count(); + if (count <= 1) return; + + await prisma.user.delete({ where: { id } }); + revalidatePath('/admin/users'); +} + +export async function changePassword(formData: FormData) { + await requireAuth(); + + const userId = formData.get('userId') as string; + const newPassword = formData.get('newPassword') as string; + const confirmPassword = formData.get('confirmPassword') as string; + + if (!userId || !newPassword) return; + if (newPassword !== confirmPassword) return; + if (newPassword.length < 6) return; + + const hashed = await bcrypt.hash(newPassword, 10); + await prisma.user.update({ + where: { id: userId }, + data: { password: hashed }, + }); + + revalidatePath('/admin/users'); +} diff --git a/app/admin/users/page.tsx b/app/admin/users/page.tsx new file mode 100644 index 0000000..f8ac195 --- /dev/null +++ b/app/admin/users/page.tsx @@ -0,0 +1,185 @@ +import { prisma } from '@/lib/db'; +import { getSession } from '@/lib/auth'; +import { redirect } from 'next/navigation'; +import { UserPlus, Trash2, KeyRound, ShieldCheck } from 'lucide-react'; +import { createUser, deleteUser, changePassword } from './actions'; + +export default async function UsersAdminPage() { + const session = await getSession(); + if (!session) redirect('/admin/login'); + + const users = await prisma.user.findMany({ + orderBy: { createdAt: 'asc' }, + }); + + return ( +
+

Kullanıcı Yönetimi

+ +
+ {/* Sol: Kullanıcı listesi */} +
+ {/* Yeni kullanıcı ekle */} +
+

+ + Yeni Kullanıcı Ekle +

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+ + {/* Mevcut kullanıcılar */} +
+

+ + Mevcut Kullanıcılar ({users.length}) +

+
+ {users.map((user: any) => ( +
+
+

{user.username}

+

+ {new Date(user.createdAt).toLocaleDateString('tr-TR', { + day: 'numeric', + month: 'long', + year: 'numeric', + })} +

+
+ {users.length > 1 && ( +
+ + +
+ )} +
+ ))} + {users.length === 0 && ( +

Kullanıcı bulunamadı.

+ )} +
+
+
+ + {/* Sağ: Şifre değiştir */} +
+

+ + Şifre Değiştir +

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+
+ ); +}