import { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { useAuth } from "@/client/components/auth-provider"; import { queryClient } from "@/client/rpc-client"; export function UserProfile() { const { user, sessionId, logout } = useAuth(); const [showPasswordChange, setShowPasswordChange] = useState(false); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [message, setMessage] = useState(""); const [error, setError] = useState(""); const { mutate: changePassword, isPending } = useMutation( queryClient.auth.changePassword.mutationOptions() ); const handlePasswordChange = (e: React.FormEvent) => { e.preventDefault(); setError(""); setMessage(""); if (newPassword !== confirmPassword) { setError("Neue Passwörter stimmen nicht überein"); return; } if (newPassword.length < 6) { setError("Neues Passwort muss mindestens 6 Zeichen lang sein"); return; } if (!sessionId) { setError("Keine gültige Sitzung"); return; } changePassword({ sessionId, currentPassword, newPassword, }, { onSuccess: () => { setMessage("Passwort erfolgreich geändert"); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); setShowPasswordChange(false); }, onError: (err) => { setError(err instanceof Error ? err.message : "Fehler beim Ändern des Passworts"); } }); }; if (!user) return null; return (
{user.username}
{user.email}
{user.role === "owner" ? "Inhaber" : "Kunde"}