import { useState } from "react"; import { useAuth } from "@/contexts/auth-context"; import { authApiClient } from "@/lib/api-client"; export function ProfileTab() { const { user } = useAuth(); const [isChangingPassword, setIsChangingPassword] = useState(false); const [passwordForm, setPasswordForm] = useState({ currentPassword: "", newPassword: "", confirmPassword: "", }); const [errors, setErrors] = useState>({}); const validatePasswordForm = (): boolean => { const newErrors: Record = {}; if (!passwordForm.currentPassword) { newErrors.currentPassword = "Current password is required"; } if (!passwordForm.newPassword) { newErrors.newPassword = "New password is required"; } else if (passwordForm.newPassword.length < 6) { newErrors.newPassword = "New password must be at least 6 characters"; } if (!passwordForm.confirmPassword) { newErrors.confirmPassword = "Please confirm your new password"; } else if (passwordForm.newPassword !== passwordForm.confirmPassword) { newErrors.confirmPassword = "Passwords do not match"; } if (passwordForm.currentPassword === passwordForm.newPassword) { newErrors.newPassword = "New password must be different from current password"; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handlePasswordChange = async (e: React.FormEvent) => { e.preventDefault(); if (!validatePasswordForm()) { return; } try { setIsChangingPassword(true); await authApiClient.changePassword( passwordForm.currentPassword, passwordForm.newPassword ); // Reset form on success setPasswordForm({ currentPassword: "", newPassword: "", confirmPassword: "", }); setErrors({}); } catch (error: any) { console.error("Password change failed:", error); // The API client will show the toast error, but we might want to handle specific field errors if (error.response?.data?.detail) { const detail = error.response.data.detail; if (detail.includes("Current password is incorrect")) { setErrors({ currentPassword: "Current password is incorrect" }); } else if (detail.includes("New password must be")) { setErrors({ newPassword: detail }); } } } finally { setIsChangingPassword(false); } }; const handleInputChange = (field: string, value: string) => { setPasswordForm(prev => ({ ...prev, [field]: value })); // Clear error for this field when user starts typing if (errors[field]) { setErrors(prev => ({ ...prev, [field]: "" })); } }; return (

Profile Settings

Manage your profile information and security settings.

{/* User Information */}

Account Information

{user?.username}

{user?.email || "Not provided"}

{user?.role === "admin" ? "Administrator" : "User"}

{user?.is_sso_user ? `SSO (${user.sso_provider})` : "Local Account"}

{/* Password Change Section - Only show for non-SSO users */} {user && !user.is_sso_user && (

Change Password

Update your password to keep your account secure.

handleInputChange("currentPassword", e.target.value)} className={`w-full px-3 py-2 rounded-lg border transition-colors ${ errors.currentPassword ? "border-error text-error-text bg-error-muted" : "border-border dark:border-border-dark bg-surface dark:bg-surface-dark text-content-primary dark:text-content-primary-dark focus:border-primary focus:ring-1 focus:ring-primary" }`} placeholder="Enter your current password" disabled={isChangingPassword} /> {errors.currentPassword && (

{errors.currentPassword}

)}
handleInputChange("newPassword", e.target.value)} className={`w-full px-3 py-2 rounded-lg border transition-colors ${ errors.newPassword ? "border-error text-error-text bg-error-muted" : "border-border dark:border-border-dark bg-surface dark:bg-surface-dark text-content-primary dark:text-content-primary-dark focus:border-primary focus:ring-1 focus:ring-primary" }`} placeholder="Enter your new password" disabled={isChangingPassword} /> {errors.newPassword && (

{errors.newPassword}

)}

Must be at least 6 characters long

handleInputChange("confirmPassword", e.target.value)} className={`w-full px-3 py-2 rounded-lg border transition-colors ${ errors.confirmPassword ? "border-error text-error-text bg-error-muted" : "border-border dark:border-border-dark bg-surface dark:bg-surface-dark text-content-primary dark:text-content-primary-dark focus:border-primary focus:ring-1 focus:ring-primary" }`} placeholder="Confirm your new password" disabled={isChangingPassword} /> {errors.confirmPassword && (

{errors.confirmPassword}

)}
)} {/* SSO User Notice */} {user?.is_sso_user && (

SSO Account

Your account is managed by {user.sso_provider}. To change your password, please use your {user.sso_provider} account settings.

)}
); }