Files
monkey-ai-web/src/components/auth/PasswordResetConfirm.jsx
T

214 lines
6.6 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useEffect, useState } from 'react';
import {
API,
copy,
showError,
showNotice,
getLogo,
getSystemName,
} from '../../helpers';
import { useSearchParams, Link } from 'react-router-dom';
import { Button, Form, Typography, Banner } from '@/components/ui';
import { IconMail, IconLock, IconCopy } from '@/components/ui/icons';
import { useTranslation } from 'react-i18next';
import AuthBackground from '../common/AuthBackground';
import GlassCard from './GlassCard';
import AuthCardHeader from './AuthCardHeader';
const { Text } = Typography;
const PasswordResetConfirm = () => {
const { t } = useTranslation();
const [inputs, setInputs] = useState({
email: '',
token: '',
});
const { email, token } = inputs;
const isValidResetLink = email && token;
const [loading, setLoading] = useState(false);
const [disableButton, setDisableButton] = useState(false);
const [countdown, setCountdown] = useState(30);
const [newPassword, setNewPassword] = useState('');
const [searchParams] = useSearchParams();
const [formApi, setFormApi] = useState(null);
const logo = getLogo();
const systemName = getSystemName();
useEffect(() => {
let token = searchParams.get('token');
let email = searchParams.get('email');
setInputs({
token: token || '',
email: email || '',
});
if (formApi) {
formApi.setValues({
email: email || '',
newPassword: newPassword || '',
});
}
}, [searchParams, newPassword, formApi]);
useEffect(() => {
let countdownInterval = null;
if (disableButton && countdown > 0) {
countdownInterval = setInterval(() => {
setCountdown(countdown - 1);
}, 1000);
} else if (countdown === 0) {
setDisableButton(false);
setCountdown(30);
}
return () => clearInterval(countdownInterval);
}, [disableButton, countdown]);
async function handleSubmit(e) {
if (!email || !token) {
showError(t('无效的重置链接,请重新发起密码重置请求'));
return;
}
setDisableButton(true);
setLoading(true);
const res = await API.post(`/api/user/reset`, {
email,
token,
});
const { success, message } = res.data;
if (success) {
let password = res.data.data;
setNewPassword(password);
// 复制发生在异步请求之后,已脱离用户点击手势,降级复制可能失败;失败时只展示密码
if (await copy(password)) {
showNotice(`${t('密码已重置并已复制到剪贴板:')} ${password}`);
} else {
showNotice(`${t('密码已重置')}:${password}`);
}
} else {
showError(message);
}
setLoading(false);
}
return (
<div
className='auth-login-page relative overflow-hidden flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8'
// 与登录页一致:抵扣 PageLayout 跑马灯占位高度,避免幽灵滚动
style={{ minHeight: 'calc(100vh - var(--content-top-offset, 0px))' }}
>
<AuthBackground />
<div className='w-full max-w-md z-10 flex flex-col items-center'>
<GlassCard>
<AuthCardHeader
logo={logo}
title={systemName}
subtitle={t('密码重置确认')}
/>
{!isValidResetLink && (
<Banner
type='danger'
description={t('无效的重置链接,请重新发起密码重置请求')}
className='auth-login-banner'
closeIcon={null}
/>
)}
<Form
className='auth-login-form'
getFormApi={(api) => setFormApi(api)}
initValues={{
email: email || '',
newPassword: newPassword || '',
}}
>
<Form.Input
field='email'
label={t('邮箱')}
name='email'
disabled={true}
prefix={<IconMail />}
placeholder={email ? '' : t('等待获取邮箱信息...')}
/>
{newPassword && (
<Form.Input
field='newPassword'
label={t('新密码')}
name='newPassword'
disabled={true}
prefix={<IconLock />}
suffix={
<Button
icon={<IconCopy />}
type='tertiary'
theme='borderless'
size='small'
onClick={async () => {
if (await copy(newPassword)) {
showNotice(
`${t('密码已复制到剪贴板:')} ${newPassword}`,
);
} else {
showError(t('复制失败,请手动复制'));
}
}}
>
{t('复制')}
</Button>
}
/>
)}
<div className='auth-login-actions'>
<Button
theme='solid'
block
className='auth-login-submit'
htmlType='submit'
onClick={handleSubmit}
loading={loading}
disabled={disableButton || newPassword || !isValidResetLink}
>
{newPassword ? t('密码重置完成') : t('确认重置密码')}
</Button>
</div>
</Form>
<div className='auth-login-register text-center'>
<Text className='text-xs'>
<Link
to='/login'
className='!text-brand-deep dark:!text-brand-from hover:!text-brand dark:hover:!text-brand-from font-semibold transition-colors'
>
{t('返回登录')}
</Link>
</Text>
</div>
</GlassCard>
</div>
</div>
);
};
export default PasswordResetConfirm;