'use client'; import React, { useState } from 'react'; import styled from 'styled-components'; import { signIn } from 'next-auth/react'; import { useRouter } from 'next/navigation'; const Container = styled.div` width: 100%; display: flex; flex-direction: column; gap: 2rem; `; const Form = styled.form` display: flex; flex-direction: column; gap: 1.5rem; width: 100%; `; const Title = styled.h2` font-size: 2rem; font-weight: 700; color: #fff; text-align: center; margin-bottom: 0.5rem; `; const Subtitle = styled.p` color: rgba(255, 255, 255, 0.7); text-align: center; margin-bottom: 2rem; font-size: 1rem; `; const InputGroup = styled.div` display: flex; flex-direction: column; gap: 0.5rem; width: 100%; `; const Label = styled.label` font-size: 0.875rem; font-weight: 500; color: rgba(255, 255, 255, 0.9); `; const Input = styled.input` width: 100%; padding: 1rem; border-radius: 12px; border: 1px solid rgba(255, 255, 255, 0.1); background: rgba(255, 255, 255, 0.05); color: #fff; font-size: 1rem; transition: all 0.2s; &::placeholder { color: rgba(255, 255, 255, 0.3); } &:focus { outline: none; border-color: #2196f3; background: rgba(255, 255, 255, 0.1); box-shadow: 0 0 0 4px rgba(33, 150, 243, 0.1); } `; const Button = styled.button` width: 100%; background: linear-gradient(to right, #2196f3, #1e88e5); color: white; padding: 1rem; border-radius: 12px; border: none; font-size: 1rem; font-weight: 600; cursor: pointer; transition: all 0.2s; margin-top: 1rem; &:hover { background: linear-gradient(to right, #1e88e5, #1976d2); transform: translateY(-1px); box-shadow: 0 4px 15px rgba(33, 150, 243, 0.3); } &:active { transform: translateY(0); } `; const Divider = styled.div` display: flex; align-items: center; text-align: center; margin: 2rem 0; &::before, &::after { content: ''; flex: 1; border-bottom: 1px solid rgba(255, 255, 255, 0.1); } &::before { margin-right: 1rem; } &::after { margin-left: 1rem; } `; const DividerText = styled.span` color: rgba(255, 255, 255, 0.5); font-size: 0.875rem; padding: 0 1rem; `; const GoogleButton = styled(Button)` background: rgba(255, 255, 255, 0.05); border: 1px solid rgba(255, 255, 255, 0.1); display: flex; align-items: center; justify-content: center; gap: 0.5rem; margin-top: 0; &:hover { background: rgba(255, 255, 255, 0.1); box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); } `; const ToggleText = styled.p` color: rgba(255, 255, 255, 0.7); text-align: center; margin-top: 2rem; button { color: #2196f3; background: none; border: none; padding: 0; margin-left: 0.5rem; font-weight: 600; cursor: pointer; &:hover { text-decoration: underline; } } `; const ErrorMessage = styled.div` color: #ff5252; font-size: 0.875rem; text-align: center; padding: 0.75rem; background: rgba(255, 82, 82, 0.1); border-radius: 8px; margin-top: 1rem; `; export default function LoginClient() { const [isLogin, setIsLogin] = useState(true); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [name, setName] = useState(''); const [error, setError] = useState(''); const router = useRouter(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); try { if (isLogin) { const result = await signIn('credentials', { redirect: false, email, password, }); if (result?.error) { if (result.error === 'EMAIL_NOT_VERIFIED') { router.push(`/verify?email=${encodeURIComponent(email)}`); return; } throw new Error(result.error); } router.push('/'); } else { const response = await fetch('/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password, name }), }); if (!response.ok) { const data = await response.json(); throw new Error(data.error || 'Ошибка при регистрации'); } const data = await response.json(); // Сохраняем пароль для автовхода после верификации localStorage.setItem('password', password); router.push(`/verify?email=${encodeURIComponent(email)}`); } } catch (err) { setError(err instanceof Error ? err.message : 'Произошла ошибка'); } }; const handleGoogleSignIn = () => { signIn('google', { callbackUrl: '/' }); }; return (
{isLogin ? 'С возвращением!' : 'Создать аккаунт'} {isLogin ? 'Войдите в свой аккаунт для доступа к фильмам' : 'Зарегистрируйтесь для доступа ко всем возможностям'}
{!isLogin && ( setName(e.target.value)} required={!isLogin} /> )} setEmail(e.target.value)} required /> setPassword(e.target.value)} required /> {error && {error}}
или Продолжить с Google {isLogin ? 'Еще нет аккаунта?' : 'Уже есть аккаунт?'}
); }