create-ex-den-kit
Version:
Create exam projects with Ex-Den-Kit
157 lines (145 loc) • 5.28 kB
JSX
import React, { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { motion } from 'framer-motion';
import { useForm } from 'react-hook-form';
import { useAuth } from '../contexts/AuthContext';
import { useTheme } from '../contexts/ThemeContext';
import { LogIn, Eye, EyeOff } from 'lucide-react';
const Login = () => {
const { login } = useAuth();
const { config } = useTheme();
const navigate = useNavigate();
const [showPassword, setShowPassword] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const {
register,
handleSubmit,
formState: { errors },
setError,
} = useForm();
const onSubmit = async (data) => {
setIsLoading(true);
const result = await login(data);
setIsLoading(false);
if (result.success) {
navigate('/dashboard');
} else {
setError('login', { message: result.error });
}
};
return (
<div className="min-h-screen bg-gradient-to-br from-gray-50 to-gray-100 flex items-center justify-center p-4">
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
className="max-w-md w-full"
>
<div className="bg-white rounded-2xl shadow-xl p-8">
<div className="text-center mb-8">
<motion.div
initial={{ scale: 0 }}
animate={{ scale: 1 }}
transition={{ delay: 0.2, type: 'spring' }}
className="text-6xl mb-4"
>
{config.icon}
</motion.div>
<h2 className="text-3xl font-bold text-gray-900">{config.name}</h2>
<p className="text-gray-600 mt-2">Войдите в систему</p>
</div>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6">
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Логин
</label>
<input
type="text"
{...register('login', {
required: 'Введите логин',
})}
className="input-field"
placeholder="Ваш логин"
/>
{errors.login && (
<p className="error-message">{errors.login.message}</p>
)}
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Пароль
</label>
<div className="relative">
<input
type={showPassword ? 'text' : 'password'}
{...register('password', {
required: 'Введите пароль',
})}
className="input-field pr-10"
placeholder="Ваш пароль"
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-700"
>
{showPassword ? <EyeOff size={20} /> : <Eye size={20} />}
</button>
</div>
{errors.password && (
<p className="error-message">{errors.password.message}</p>
)}
</div>
<motion.button
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
type="submit"
disabled={isLoading}
className="w-full btn-primary flex items-center justify-center space-x-2"
>
{isLoading ? (
<motion.div
animate={{ rotate: 360 }}
transition={{ duration: 1, repeat: Infinity, ease: 'linear' }}
className="w-5 h-5 border-2 border-white border-t-transparent rounded-full"
/>
) : (
<>
<LogIn size={20} />
<span>Войти</span>
</>
)}
</motion.button>
</form>
<div className="mt-6 text-center space-y-2">
<p className="text-gray-600">
Нет аккаунта?{' '}
<Link to="/register" className="text-primary hover:underline font-medium">
Зарегистрироваться
</Link>
</p>
<p className="text-gray-500 text-sm">
<Link to="/admin/login" className="hover:text-primary transition-colors">
Вход для администратора →
</Link>
</p>
</div>
</div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.5 }}
className="text-center mt-4"
>
<Link
to="/theme"
className="text-gray-500 hover:text-primary transition-colors text-sm"
>
← Сменить тему
</Link>
</motion.div>
</motion.div>
</div>
);
};
export default Login;