auth
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Alert, Button, Card, Form, Input, Typography } from 'antd'
|
||||
import { MailOutlined, SafetyOutlined } from '@ant-design/icons'
|
||||
import { loginByEmailCode, requestCode, AuthError } from '@/auth/authStore'
|
||||
|
||||
const RESEND_COOLDOWN_SECONDS = 60
|
||||
|
||||
/**
|
||||
* Вход по email-коду: код приходит на почту (в dev без SMTP бэкенд пишет его в лог).
|
||||
*/
|
||||
export function LoginScreen() {
|
||||
const [step, setStep] = useState<'email' | 'code'>('email')
|
||||
const [email, setEmail] = useState('')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [cooldown, setCooldown] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
if (cooldown <= 0) return
|
||||
const timer = setInterval(() => setCooldown((c) => c - 1), 1000)
|
||||
return () => clearInterval(timer)
|
||||
}, [cooldown])
|
||||
|
||||
const submitEmail = async (values: { email: string }) => {
|
||||
setBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
await requestCode(values.email)
|
||||
setEmail(values.email)
|
||||
setStep('code')
|
||||
setCooldown(RESEND_COOLDOWN_SECONDS)
|
||||
} catch (e) {
|
||||
setError(describe(e))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const submitCode = async (values: { code: string }) => {
|
||||
setBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
await loginByEmailCode(email, values.code)
|
||||
// Сессия появится в AuthContext через подписку authStore
|
||||
} catch (e) {
|
||||
setError(describe(e))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
minHeight: '100vh',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
background: '#f0f2f5',
|
||||
}}
|
||||
>
|
||||
<Card style={{ width: 400 }}>
|
||||
<Typography.Title level={3} style={{ textAlign: 'center', marginTop: 0 }}>
|
||||
Городар · Админ-панель
|
||||
</Typography.Title>
|
||||
<Typography.Paragraph type="secondary" style={{ textAlign: 'center' }}>
|
||||
{step === 'email'
|
||||
? 'Введите рабочий email — пришлём код подтверждения'
|
||||
: `Код отправлен на ${email}`}
|
||||
</Typography.Paragraph>
|
||||
|
||||
{error && (
|
||||
<Alert type="error" showIcon message={error} style={{ marginBottom: 16 }} closable />
|
||||
)}
|
||||
|
||||
{step === 'email' ? (
|
||||
<Form layout="vertical" onFinish={submitEmail} requiredMark={false}>
|
||||
<Form.Item
|
||||
name="email"
|
||||
rules={[
|
||||
{ required: true, message: 'Укажите email' },
|
||||
{ type: 'email', message: 'Некорректный email' },
|
||||
]}
|
||||
>
|
||||
<Input prefix={<MailOutlined />} placeholder="email@example.com" size="large" autoFocus />
|
||||
</Form.Item>
|
||||
<Button type="primary" htmlType="submit" block size="large" loading={busy}>
|
||||
Получить код
|
||||
</Button>
|
||||
</Form>
|
||||
) : (
|
||||
<Form layout="vertical" onFinish={submitCode} requiredMark={false}>
|
||||
<Form.Item
|
||||
name="code"
|
||||
rules={[
|
||||
{ required: true, message: 'Введите код из письма' },
|
||||
{ pattern: /^\d{6}$/, message: 'Код — 6 цифр' },
|
||||
]}
|
||||
>
|
||||
<Input
|
||||
prefix={<SafetyOutlined />}
|
||||
placeholder="000000"
|
||||
size="large"
|
||||
autoFocus
|
||||
maxLength={6}
|
||||
inputMode="numeric"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Button type="primary" htmlType="submit" block size="large" loading={busy}>
|
||||
Войти
|
||||
</Button>
|
||||
<Button
|
||||
type="link"
|
||||
block
|
||||
disabled={cooldown > 0 || busy}
|
||||
onClick={() => submitEmail({ email })}
|
||||
>
|
||||
{cooldown > 0 ? `Отправить код повторно (${cooldown} с)` : 'Отправить код повторно'}
|
||||
</Button>
|
||||
<Button type="text" block onClick={() => setStep('email')}>
|
||||
Изменить email
|
||||
</Button>
|
||||
</Form>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function describe(e: unknown): string {
|
||||
if (e instanceof AuthError) return e.description
|
||||
if (e instanceof Error) return e.message
|
||||
return 'Ошибка авторизации'
|
||||
}
|
||||
Reference in New Issue
Block a user