'use client' import { useEffect, useState } from 'react' import Link from 'next/link' import { useRouter } from 'next/navigation' import { Mail, Lock, Loader2, Sparkles, AlertCircle } from 'lucide-react' import { useAuth } from '@/context/AuthContext' import { login as authLogin } from '@/lib/auth' import { cn } from '@/lib/utils' export default function LoginPage() { const router = useRouter() const { refreshUser, isAuthenticated } = useAuth() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) // Redirect if already authenticated (client-side effect to avoid rendering push) useEffect(() => { if (isAuthenticated) { router.push('/') } }, [isAuthenticated, router]) if (isAuthenticated) { return null } const validateForm = (currentEmail: string, currentPassword: string): string | null => { if (!currentEmail.trim()) { return 'Введіть email адресу' } // Basic email validation const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ if (!emailRegex.test(currentEmail)) { return 'Введіть коректну email адресу' } if (!currentPassword) { return 'Введіть пароль' } if (currentPassword.length < 8) { return 'Пароль повинен містити мінімум 8 символів' } return null } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') const formData = new FormData(e.currentTarget) const fallbackEmail = (formData.get('email')?.toString() ?? '').trim() const fallbackPassword = formData.get('password')?.toString() ?? '' const currentEmail = email.trim() || fallbackEmail const currentPassword = password || fallbackPassword if (!email && currentEmail) setEmail(currentEmail) if (!password && currentPassword) setPassword(currentPassword) const validationError = validateForm(currentEmail, currentPassword) if (validationError) { setError(validationError) return } setLoading(true) try { await authLogin(currentEmail, currentPassword) await refreshUser() router.push('/') } catch (err) { setError(err instanceof Error ? err.message : 'Помилка входу. Перевірте дані та спробуйте ще раз.') } finally { setLoading(false) } } return (
{/* Logo */}
DAARION

Вхід в акаунт

Увійдіть, щоб продовжити у DAARION.city

{/* Form */}
{/* Error */} {error && (
{error}
)} {/* Email */}
setEmail(e.target.value)} placeholder="your@email.com" autoComplete="email" className={cn( 'w-full pl-10 pr-4 py-3 bg-slate-800/50 border border-white/10 rounded-xl', 'text-white placeholder-slate-500', 'focus:outline-none focus:border-cyan-500/50 focus:ring-1 focus:ring-cyan-500/20', 'transition-all' )} />
{/* Password */}
setPassword(e.target.value)} placeholder="••••••••" autoComplete="current-password" className={cn( 'w-full pl-10 pr-4 py-3 bg-slate-800/50 border border-white/10 rounded-xl', 'text-white placeholder-slate-500', 'focus:outline-none focus:border-cyan-500/50 focus:ring-1 focus:ring-cyan-500/20', 'transition-all' )} />
{/* Submit */}
{/* Divider */}
або
{/* Register link */}

Немає акаунту?{' '} Зареєструватися

{/* Back to home */}

← Повернутися на головну

) }