2025-12-21 20:40:32 +01:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
import { Link, useNavigate } from 'react-router-dom';
|
|
|
|
|
import { supabase, isSupabaseConfigured } from '../../lib/supabaseClient';
|
|
|
|
|
import { Button } from '../../components/Button';
|
|
|
|
|
import { useAuth } from '../../context/AuthContext';
|
2025-12-22 17:59:43 +01:00
|
|
|
import { LogIn, AlertCircle, ArrowLeft, RefreshCw, Mail } from 'lucide-react';
|
2025-12-21 20:40:32 +01:00
|
|
|
|
|
|
|
|
export const Login: React.FC = () => {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const { refreshDemoUser } = useAuth();
|
|
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const [needsConfirmation, setNeedsConfirmation] = useState(false);
|
|
|
|
|
const [resendLoading, setResendLoading] = useState(false);
|
|
|
|
|
const [resendSuccess, setResendSuccess] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleLogin = async (e: React.FormEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
setNeedsConfirmation(false);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!isSupabaseConfigured) {
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
|
const demoUser = {
|
|
|
|
|
user: {
|
|
|
|
|
id: 'demo-user-123',
|
|
|
|
|
email: email,
|
|
|
|
|
user_metadata: { date_of_birth: '1990-01-01' }
|
|
|
|
|
},
|
|
|
|
|
access_token: 'demo-token',
|
|
|
|
|
};
|
|
|
|
|
localStorage.setItem('demo_user_session', JSON.stringify(demoUser));
|
|
|
|
|
refreshDemoUser();
|
|
|
|
|
navigate('/dashboard');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { error } = await supabase.auth.signInWithPassword({
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error('Login error:', error);
|
2025-12-22 17:59:43 +01:00
|
|
|
|
|
|
|
|
// Specifikus hibaüzenetek kezelése
|
2025-12-21 20:40:32 +01:00
|
|
|
if (error.message.includes('Invalid login credentials')) {
|
2025-12-22 17:59:43 +01:00
|
|
|
setError('Helytelen e-mail cím vagy jelszó. Ha most regisztráltál, ellenőrizd a postafiókodat és kattints a megerősítő linkre!');
|
2025-12-21 20:40:32 +01:00
|
|
|
setNeedsConfirmation(true);
|
|
|
|
|
} else if (error.message.includes('Email not confirmed')) {
|
2025-12-22 17:59:43 +01:00
|
|
|
setError('Az e-mail címed még nincs megerősítve. Kérjük, kattints a regisztrációkor kapott linkre!');
|
2025-12-21 20:40:32 +01:00
|
|
|
setNeedsConfirmation(true);
|
|
|
|
|
} else {
|
2025-12-22 17:59:43 +01:00
|
|
|
setError('Hiba történt: ' + error.message);
|
2025-12-21 20:40:32 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
navigate('/dashboard');
|
|
|
|
|
}
|
|
|
|
|
} catch (err: any) {
|
2025-12-22 17:59:43 +01:00
|
|
|
setError('Váratlan hiba történt a bejelentkezés során.');
|
2025-12-21 20:40:32 +01:00
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleResendConfirmation = async () => {
|
|
|
|
|
if (!email) {
|
|
|
|
|
setError('Kérjük, először adja meg az e-mail címét!');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setResendLoading(true);
|
|
|
|
|
setResendSuccess(false);
|
|
|
|
|
setError(null);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const redirectUrl = `${window.location.origin}/#/auth/login`;
|
|
|
|
|
const { error } = await supabase.auth.resend({
|
|
|
|
|
type: 'signup',
|
|
|
|
|
email: email,
|
|
|
|
|
options: { emailRedirectTo: redirectUrl }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
setError('Hiba az újraküldéskor: ' + error.message);
|
|
|
|
|
} else {
|
|
|
|
|
setResendSuccess(true);
|
2025-12-22 17:59:43 +01:00
|
|
|
setNeedsConfirmation(false);
|
2025-12-21 20:40:32 +01:00
|
|
|
}
|
|
|
|
|
} catch (err: any) {
|
2025-12-22 17:59:43 +01:00
|
|
|
setError('Hiba történt az e-mail újraküldésekor.');
|
2025-12-21 20:40:32 +01:00
|
|
|
} finally {
|
|
|
|
|
setResendLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="min-h-screen pt-20 pb-12 bg-gray-50 flex flex-col items-center justify-center px-4 sm:px-6 lg:px-8">
|
|
|
|
|
<div className="w-full max-w-md">
|
|
|
|
|
<div className="text-center mb-8">
|
|
|
|
|
<h2 className="text-3xl font-extrabold text-gray-900">Bejelentkezés</h2>
|
2025-12-22 17:59:43 +01:00
|
|
|
<p className="mt-2 text-sm text-gray-600 font-medium">Lépjen be fiókjába a kezeléshez.</p>
|
2025-12-21 20:40:32 +01:00
|
|
|
</div>
|
|
|
|
|
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="bg-white py-8 px-4 shadow-xl rounded-[32px] sm:px-10 border border-gray-100">
|
2025-12-21 20:40:32 +01:00
|
|
|
<form className="space-y-6" onSubmit={handleLogin}>
|
|
|
|
|
{error && (
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="rounded-2xl bg-red-50 p-4 border border-red-100 flex flex-col animate-fade-in">
|
2025-12-21 20:40:32 +01:00
|
|
|
<div className="flex items-start">
|
|
|
|
|
<div className="flex-shrink-0"><AlertCircle className="h-5 w-5 text-red-400" /></div>
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="ml-3"><p className="text-xs font-bold text-red-800 uppercase tracking-tight">{error}</p></div>
|
2025-12-21 20:40:32 +01:00
|
|
|
</div>
|
|
|
|
|
{needsConfirmation && (
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="mt-3 pt-3 border-t border-red-100 w-full">
|
|
|
|
|
<button type="button" onClick={handleResendConfirmation} disabled={resendLoading} className="text-xs font-black text-primary hover:underline flex items-center gap-2 uppercase tracking-widest">
|
|
|
|
|
{resendLoading ? <RefreshCw className="w-3 h-3 animate-spin" /> : <Mail className="w-3 h-3" />}
|
|
|
|
|
Megerősítő levél újraküldése
|
2025-12-21 20:40:32 +01:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{resendSuccess && (
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="rounded-2xl bg-green-50 p-4 border border-green-100 flex items-start animate-fade-in shadow-sm">
|
2025-12-21 20:40:32 +01:00
|
|
|
<div className="flex-shrink-0"><Mail className="h-5 w-5 text-green-400" /></div>
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="ml-3"><p className="text-xs font-bold text-green-800 uppercase tracking-tight">A megerősítő linket újra elküldtük!</p></div>
|
2025-12-21 20:40:32 +01:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="space-y-4">
|
|
|
|
|
<div>
|
|
|
|
|
<label htmlFor="email" className="block text-xs font-black text-gray-400 uppercase tracking-widest mb-1">E-mail cím</label>
|
|
|
|
|
<input id="email" type="email" required value={email} onChange={(e) => setEmail(e.target.value)} className="appearance-none block w-full px-4 py-3 border border-gray-200 rounded-xl shadow-sm focus:outline-none focus:ring-4 focus:ring-primary/10 focus:border-primary sm:text-sm bg-white text-gray-900 font-medium" placeholder="pelda@email.hu" />
|
2025-12-21 20:40:32 +01:00
|
|
|
</div>
|
|
|
|
|
|
2025-12-22 17:59:43 +01:00
|
|
|
<div>
|
|
|
|
|
<div className="flex justify-between items-center mb-1">
|
|
|
|
|
<label htmlFor="password" className="block text-xs font-black text-gray-400 uppercase tracking-widest">Jelszó</label>
|
2025-12-26 14:03:18 +01:00
|
|
|
{/* Fixed: removed invalid 'size' prop from Link component */}
|
|
|
|
|
<Link to="/auth/forgot-password" className="text-[10px] font-black text-primary hover:underline uppercase tracking-widest">Elfelejtette?</Link>
|
2025-12-22 17:59:43 +01:00
|
|
|
</div>
|
|
|
|
|
<input id="password" type="password" required value={password} onChange={(e) => setPassword(e.target.value)} className="appearance-none block w-full px-4 py-3 border border-gray-200 rounded-xl shadow-sm focus:outline-none focus:ring-4 focus:ring-primary/10 focus:border-primary sm:text-sm bg-white text-gray-900 font-medium" placeholder="••••••••" />
|
2025-12-21 20:40:32 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-12-22 17:59:43 +01:00
|
|
|
<Button type="submit" fullWidth disabled={loading} size="lg" className="shadow-lg shadow-primary/20">
|
2025-12-21 20:40:32 +01:00
|
|
|
{loading ? <RefreshCw className="w-5 h-5 animate-spin" /> : <>Bejelentkezés <LogIn className="ml-2 w-4 h-4" /></>}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
|
2025-12-22 17:59:43 +01:00
|
|
|
<div className="mt-8 pt-6 border-t border-gray-100 text-center">
|
|
|
|
|
<p className="text-sm text-gray-500 font-medium mb-2">Nincs még fiókod?</p>
|
|
|
|
|
<Link to="/auth/register" className="text-sm font-bold text-primary hover:text-secondary transition-colors underline-offset-4 hover:underline">Regisztrálj ingyenesen</Link>
|
2025-12-21 20:40:32 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="mt-8 text-center">
|
2025-12-22 17:59:43 +01:00
|
|
|
<Link to="/" className="text-sm font-bold text-gray-400 hover:text-gray-900 flex items-center justify-center group transition-colors">
|
|
|
|
|
<ArrowLeft className="w-4 h-4 mr-2 group-hover:-translate-x-1 transition-transform" /> Vissza a főoldalra
|
2025-12-21 20:40:32 +01:00
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|