"use client"; import { useState, useTransition } from "react"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Separator } from "@/components/ui/separator"; import { User, Lock, Shield } from "lucide-react"; import { toast } from "sonner"; import { authClient, useSession } from "@/lib/auth/client"; export default function SettingsPage() { const { data: session, isPending } = useSession(); const [name, setName] = useState(""); const [nameLoading, startNameTransition] = useTransition(); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [passwordLoading, startPasswordTransition] = useTransition(); // Populate name field once session loads const displayName = name || session?.user?.name || ""; function handleNameSave() { if (!displayName.trim()) { toast.error("Name cannot be empty"); return; } startNameTransition(async () => { const { error } = await authClient.updateUser({ name: displayName.trim() }); if (error) { toast.error(error.message ?? "Failed to update name"); } else { toast.success("Display name updated"); } }); } function handlePasswordChange() { if (!currentPassword) { toast.error("Current password is required"); return; } if (newPassword.length < 8) { toast.error("New password must be at least 8 characters"); return; } if (newPassword !== confirmPassword) { toast.error("Passwords do not match"); return; } startPasswordTransition(async () => { const { error } = await authClient.changePassword({ currentPassword, newPassword, revokeOtherSessions: false, }); if (error) { toast.error(error.message ?? "Failed to change password"); } else { toast.success("Password changed successfully"); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); } }); } return (

Account Settings

Manage your profile and security preferences

{/* Profile */} Profile Update your display name and view account info

Email address cannot be changed

setName(e.target.value)} placeholder="Your name" className="bg-zinc-800 border-zinc-700 text-white placeholder:text-zinc-500" disabled={isPending} />
{session?.user && (
Role: {(session.user as { role?: string }).role ?? "moderator"}
)}
{/* Password */} Change Password Choose a strong password with at least 8 characters
setCurrentPassword(e.target.value)} placeholder="••••••••" className="bg-zinc-800 border-zinc-700 text-white placeholder:text-zinc-500" />
setNewPassword(e.target.value)} placeholder="••••••••" className="bg-zinc-800 border-zinc-700 text-white placeholder:text-zinc-500" />
setConfirmPassword(e.target.value)} placeholder="••••••••" className="bg-zinc-800 border-zinc-700 text-white placeholder:text-zinc-500" />
); }