Files
platform-project/platform/frontend/src/pages/Login.tsx
T
Арсентьев Александр 07dbde2a0e Initial monorepo structure
2026-08-06 11:23:37 +03:00

60 lines
2.5 KiB
TypeScript
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.
import { useState, useEffect } from 'react'
import { useNavigate, Link } from 'react-router-dom'
import { useAuth } from '../contexts/AuthContext'
export default function Login() {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
const { user, login } = useAuth()
const navigate = useNavigate()
// If already logged in, redirect to home
useEffect(() => {
if (user) {
navigate('/')
}
}, [user, navigate])
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setError('')
setLoading(true)
try {
await login(email, password)
navigate('/')
} catch (err: any) {
setError(err.response?.data?.detail || 'Ошибка входа')
} finally {
setLoading(false)
}
}
return (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<div className="bg-white p-8 rounded-lg shadow-md w-full max-w-md">
<h1 className="text-2xl font-bold text-center mb-6">Платформа</h1>
<h2 className="text-lg text-gray-600 text-center mb-8">Войдите в аккаунт</h2>
{error && <div className="bg-red-50 text-red-600 p-3 rounded-lg mb-4 text-sm">{error}</div>}
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" value={email} onChange={e => setEmail(e.target.value)} className="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" required />
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">Пароль</label>
<input type="password" value={password} onChange={e => setPassword(e.target.value)} className="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" required />
</div>
<button type="submit" disabled={loading} className="w-full py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50">
{loading ? 'Вход...' : 'Войти'}
</button>
</form>
<p className="mt-4 text-center text-sm text-gray-600">
Нет аккаунта? <Link to="/register" className="text-blue-600 hover:underline">Регистрация</Link>
</p>
</div>
</div>
)
}