import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Form, Input, Button, Card, Space, Typography, Alert } from 'antd'; import { UserOutlined, LockOutlined, CheckSquareOutlined, MailOutlined } from '@ant-design/icons'; import { api } from '../api/axios'; const { Title, Text, Link } = Typography; function Register() { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const navigate = useNavigate(); const onFinish = async (values: any) => { setLoading(true); setError(null); try { await api.post('/auth/register', { username: values.username, email: values.email, password: values.password, }); navigate('/login'); } catch (err: any) { const errorMessage = err.response?.data?.message || '注册失败,用户名可能已被占用或服务器错误。'; setError(errorMessage); } finally { setLoading(false); } }; return (
Todo List 轻松管理您的任务
{error && ( setError(null)} style={{ marginBottom: 24 }} /> )}
} placeholder="用户名" size="large" /> } placeholder="邮箱" size="large" /> } placeholder="密码" size="large" /> ({ validator(_, value) { if (!value || getFieldValue('password') === value) { return Promise.resolve(); } return Promise.reject(new Error('两次输入的密码不一致!')); }, }), ]} > } placeholder="确认密码" size="large" />
已有账号?去登录
); } export default Register;