feat(auth): enhance password change flow and error handling
- Return response from useProfileSetting hook after password change
This commit is contained in:
@@ -224,7 +224,7 @@ const Login = () => {
|
||||
fullWidth
|
||||
id="register-confirm-password"
|
||||
type={showConfirmPassword ? 'text' : 'password'}
|
||||
placeholder={t('confirmPassword')}
|
||||
placeholder={t('login.confirmPassword')}
|
||||
autoComplete="new-password"
|
||||
{...registerForm.register('confirmPassword', registerValidation.confirmPassword)}
|
||||
error={!!registerForm.formState.errors.confirmPassword}
|
||||
|
||||
@@ -20,7 +20,8 @@ import logger from '@/utils/logger';
|
||||
interface ChangePasswordDialogProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
changeUserPassword: (data: { password: string; new_password: string }) => Promise<void>;
|
||||
onSuccess: () => void;
|
||||
changeUserPassword: (data: { password: string; new_password: string }) => Promise<any>;
|
||||
}
|
||||
|
||||
interface PasswordFormData {
|
||||
@@ -32,7 +33,7 @@ interface PasswordFormData {
|
||||
/**
|
||||
* 修改密码对话框
|
||||
*/
|
||||
function ChangePasswordDialog({ open, onClose, changeUserPassword }: ChangePasswordDialogProps) {
|
||||
function ChangePasswordDialog({ open, onClose, onSuccess, changeUserPassword }: ChangePasswordDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const { showMessage } = useSnackbar();
|
||||
|
||||
@@ -133,20 +134,19 @@ function ChangePasswordDialog({ open, onClose, changeUserPassword }: ChangePassw
|
||||
|
||||
setLoading(true);
|
||||
try {
|
||||
await changeUserPassword({
|
||||
const res = await changeUserPassword({
|
||||
password: formData.currentPassword,
|
||||
new_password: formData.newPassword
|
||||
});
|
||||
|
||||
logger.info('修改密码成功:', res);
|
||||
|
||||
showMessage.success(t('setting.passwordChangeSuccess'));
|
||||
handleClose();
|
||||
// delay 1000 ms
|
||||
setTimeout(() => onSuccess(), 1000);
|
||||
} catch (error: any) {
|
||||
logger.error('修改密码失败:', error);
|
||||
if (error.response?.status === 400) {
|
||||
showMessage.error(t('setting.currentPasswordIncorrect'));
|
||||
} else {
|
||||
showMessage.error(t('setting.passwordChangeError'));
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -217,7 +217,7 @@ function ChangePasswordDialog({ open, onClose, changeUserPassword }: ChangePassw
|
||||
value={formData.newPassword}
|
||||
onChange={handleInputChange('newPassword')}
|
||||
error={!!errors.newPassword}
|
||||
helperText={errors.newPassword || '密码长度至少6位'}
|
||||
helperText={errors.newPassword}
|
||||
InputProps={{
|
||||
endAdornment: (
|
||||
<InputAdornment position="end">
|
||||
|
||||
@@ -7,11 +7,14 @@ import ChangePasswordDialog from "./components/ChangePasswordDialog";
|
||||
import { useProfileSetting } from "@/hooks/setting-hooks";
|
||||
import logger from "@/utils/logger";
|
||||
import { useUserData } from "@/hooks/useUserData";
|
||||
import { useAuth } from '@/hooks/login-hooks';
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
function ProfileSetting() {
|
||||
const { t } = useTranslation();
|
||||
const { userInfo, updateUserInfo: updateUserInfoFunc, changeUserPassword: changeUserPasswordFunc } = useProfileSetting();
|
||||
const { refreshUserData } = useUserData();
|
||||
const { logout } = useAuth();
|
||||
const [passwordDialogOpen, setPasswordDialogOpen] = useState(false);
|
||||
|
||||
logger.debug('userInfo', userInfo);
|
||||
@@ -66,6 +69,10 @@ function ProfileSetting() {
|
||||
<ChangePasswordDialog
|
||||
open={passwordDialogOpen}
|
||||
onClose={handleClosePasswordDialog}
|
||||
onSuccess={() => {
|
||||
// 使用 useAuth 的 logout,确保清除本地存储令牌并跳转登录
|
||||
logout();
|
||||
}}
|
||||
changeUserPassword={changeUserPasswordFunc}
|
||||
/>
|
||||
</Box>
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
ExitToApp as ExitToAppIcon
|
||||
} from '@mui/icons-material';
|
||||
import { useTeamSetting } from '@/hooks/setting-hooks';
|
||||
import { useDialog } from '@/hooks/useDialog';
|
||||
|
||||
interface TenantUser {
|
||||
id: string;
|
||||
@@ -88,8 +89,15 @@ function TeamsSetting() {
|
||||
}
|
||||
};
|
||||
|
||||
const dialog = useDialog();
|
||||
|
||||
const handleDeleteUser = async (userId: string) => {
|
||||
await deleteUser(userId);
|
||||
dialog.confirm({
|
||||
content: t('setting.sureDelete'),
|
||||
onConfirm: async () => {
|
||||
await deleteUser(userId);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handleAgreeTenant = async (tenantId: string) => {
|
||||
|
||||
Reference in New Issue
Block a user