import React, { useState } from 'react'; import { Button, PasswordInput, Group, Alert, LoadingOverlay, Modal, Divider } from '@mantine/core'; import { useTranslation } from 'react-i18next'; import { useAuth } from '@app/auth/UseSession'; import { supabase } from '@app/auth/supabase'; import { Z_INDEX_OVER_SETTINGS_MODAL } from '@app/styles/zIndex'; const PasswordSecurity: React.FC = () => { const { t } = useTranslation(); const { refreshSession } = useAuth(); const [opened, setOpened] = useState(false); const [isLoading, setIsLoading] = useState(false); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [didUpdate, setDidUpdate] = useState(false); const handleChangePassword = async () => { if (!newPassword || !confirmPassword) { setError(t('signup.pleaseFillAllFields', 'Please fill in all fields')); return; } if (newPassword.length < 6) { setError(t('signup.passwordTooShort', 'Password must be at least 6 characters long')); return; } if (newPassword !== confirmPassword) { setError(t('signup.passwordsDoNotMatch', 'Passwords do not match')); return; } try { setIsLoading(true); setError(null); setSuccess(null); // Update to the new password directly const { error: updateError } = await supabase.auth.updateUser({ password: newPassword }); if (updateError) { setError(updateError.message); return; } setSuccess(t('login.passwordUpdatedSuccess', 'Your password has been updated successfully.')); setNewPassword(''); setConfirmPassword(''); setDidUpdate(true); // Replace form with success text, then close after 2s setTimeout(() => { // refresh session after closing to avoid UI jank void refreshSession(); setOpened(false); setDidUpdate(false); }, 2000); } catch (e: unknown) { setError(e instanceof Error ? e.message : 'Failed to change password'); } finally { setIsLoading(false); } }; return (

{t('config.account.security.title', 'Passwords & Security')}

{t('config.account.security.description', 'Manage your password and security settings.')}

setOpened(false)} centered title={t('config.account.security.changePassword', 'Change password')} zIndex={Z_INDEX_OVER_SETTINGS_MODAL}> {error && ( {error} )} {didUpdate ? ( {success || t('login.passwordUpdatedSuccess', 'Your password has been updated successfully.')} ) : (
setNewPassword(e.currentTarget.value)} /> setConfirmPassword(e.currentTarget.value)} />
)}
); }; export default PasswordSecurity;