forked from linkong/planet
release: bump version to 0.52.0
This commit is contained in:
1 parent
b15d097b9c
commit
b87cb310fd
70 files changed
+5589
-2187
No files matched your search
@@ -0,0 +1,121 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Button, Form, Input, message, Typography } from 'antd'
|
||||
import { MailOutlined, SafetyCertificateOutlined } from '@ant-design/icons'
|
||||
import axios from 'axios'
|
||||
import { useNavigate, useSearchParams } from 'react-router-dom'
|
||||
import { useAuthStore } from '../../stores/auth'
|
||||
|
||||
const API_URL = (import.meta as any).env?.VITE_API_URL || '/api/v1'
|
||||
|
||||
interface ErrorBody {
|
||||
response?: {
|
||||
data?: { detail?: string | { code?: string; message?: string; retry_after_seconds?: number } }
|
||||
}
|
||||
}
|
||||
|
||||
function extractDetail(error: unknown): string {
|
||||
const err = error as ErrorBody
|
||||
const detail = err.response?.data?.detail
|
||||
if (typeof detail === 'string') return detail
|
||||
if (detail && typeof detail === 'object') return detail.message || detail.code || '操作失败'
|
||||
return '操作失败'
|
||||
}
|
||||
|
||||
function VerifyEmail() {
|
||||
const navigate = useNavigate()
|
||||
const [search] = useSearchParams()
|
||||
const [email, setEmail] = useState(search.get('email') || '')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [resending, setResending] = useState(false)
|
||||
const [cooldown, setCooldown] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
if (cooldown <= 0) return
|
||||
const timer = setTimeout(() => setCooldown((v) => v - 1), 1000)
|
||||
return () => clearTimeout(timer)
|
||||
}, [cooldown])
|
||||
|
||||
const onVerify = async (values: { code: string }) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/auth/verify-email`, { email, code: values.code })
|
||||
const { access_token, user } = response.data
|
||||
useAuthStore.setState({ token: access_token, user })
|
||||
axios.defaults.headers.common.Authorization = `Bearer ${access_token}`
|
||||
message.success('邮箱验证成功,正在登录…')
|
||||
navigate('/admin', { replace: true })
|
||||
} catch (error) {
|
||||
message.error(extractDetail(error))
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const onResend = async () => {
|
||||
if (!email || cooldown > 0) return
|
||||
setResending(true)
|
||||
try {
|
||||
await axios.post(`${API_URL}/auth/resend-code`, { email, purpose: 'register' })
|
||||
setCooldown(60)
|
||||
message.success('验证码已重发')
|
||||
} catch (error) {
|
||||
const err = error as ErrorBody
|
||||
const detail = err.response?.data?.detail
|
||||
if (detail && typeof detail === 'object' && detail.retry_after_seconds) {
|
||||
setCooldown(detail.retry_after_seconds)
|
||||
}
|
||||
message.error(extractDetail(error))
|
||||
} finally {
|
||||
setResending(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="login-container">
|
||||
<div className="login-box">
|
||||
<h1 style={{ textAlign: 'center', marginBottom: 24 }}>验证邮箱</h1>
|
||||
<Form name="verify-email" onFinish={onVerify} layout="vertical">
|
||||
<Form.Item label="邮箱" required>
|
||||
<Input
|
||||
prefix={<MailOutlined />}
|
||||
size="large"
|
||||
value={email}
|
||||
onChange={(event) => setEmail(event.target.value)}
|
||||
placeholder="注册时使用的邮箱"
|
||||
autoComplete="email"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="code"
|
||||
label="验证码"
|
||||
rules={[
|
||||
{ required: true, message: '请输入验证码' },
|
||||
{ len: 6, message: '验证码为 6 位数字' },
|
||||
]}
|
||||
>
|
||||
<Input
|
||||
prefix={<SafetyCertificateOutlined />}
|
||||
placeholder="6 位验证码"
|
||||
size="large"
|
||||
maxLength={6}
|
||||
inputMode="numeric"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item>
|
||||
<Button type="primary" htmlType="submit" size="large" block loading={loading} disabled={!email}>
|
||||
验证并登录
|
||||
</Button>
|
||||
</Form.Item>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
|
||||
<Typography.Link onClick={() => navigate('/login')}>返回登录</Typography.Link>
|
||||
<Button type="link" size="small" disabled={cooldown > 0 || resending || !email} loading={resending} onClick={onResend}>
|
||||
{cooldown > 0 ? `重发 (${cooldown}s)` : '重新发送验证码'}
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default VerifyEmail
|
||||
Reference in new issue
Block a user