import React, { useState } from 'react'; import { Eye, EyeOff, Mail, Lock, User, ArrowLeft, AlertCircle } from 'lucide-react'; import { useAuth } from './useAuth'; // Adjust the import path as needed const Login = () => { const [isLogin, setIsLogin] = useState(true); const [showPassword, setShowPassword] = useState(false); const [showForgotPassword, setShowForgotPassword] = useState(false); const [forgotPasswordEmail, setForgotPasswordEmail] = useState(''); const [formData, setFormData] = useState({ email: '', password: '', confirmPassword: '', username: '' }); const { isLoading, error, handleLogin, handleSignUp, handleForgotPassword, setError } = useAuth(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); // Clear previous errors // Validation if (!isLogin && formData.password !== formData.confirmPassword) { setError('Passwords do not match'); return; } if (!isLogin && formData.password.length < 6) { setError('Password must be at least 6 characters long'); return; } try { let result; if (isLogin) { result = await handleLogin(formData.email, formData.password); } else { result = await handleSignUp(formData.email, formData.password); } if (result.success) { // Redirect or handle successful authentication console.log('Authentication successful:', result.data); // You can redirect here or handle the successful login window.location.href = '/'; // Example redirect } else { // Error is already set in the hook, no need to set it again console.error('Authentication failed:', result.error); } } catch (err) { console.error('Authentication error:', err); } }; const handleForgotPasswordSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); if (!forgotPasswordEmail) { setError('Please enter your email address'); return; } const result = await handleForgotPassword(forgotPasswordEmail); if (result.success) { setError(''); // Clear any previous errors alert(result.message); // Or show a success message in a better way setShowForgotPassword(false); setForgotPasswordEmail(''); } }; const handleChange = (e: React.ChangeEvent) => { setFormData(prev => ({ ...prev, [e.target.name]: e.target.value })); }; // Forgot Password Modal if (showForgotPassword) { return (
{/* Back Button */} {/* Header */}

Reset Your Password

Enter your email address and we'll send you a link to reset your password.

{/* Error Display */} {error && (
{error}
)} {/* Forgot Password Form */}
setForgotPasswordEmail(e.target.value)} className="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-[#4285F4] focus:border-[#4285F4] transition-colors" placeholder="Enter your email" required />
); } return (
{/* Header */}

{isLogin ? 'Welcome Back' : 'Create Account'}

{isLogin ? 'Sign in to your account' : 'Create your account'}

{/* Auth Card */}
{/* Error Display */} {error && (
{error}
)}
{/* Username Field - Only for Signup */} {!isLogin && (
)} {/* Email Field */}
{/* Password Field */}
{/* Confirm Password Field - Only for Signup */} {!isLogin && (
)} {/* Forgot Password - Only for Login */} {isLogin && (
)} {/* Submit Button */} {/* Divider */}
Or continue with
{/* Social Login Buttons */}
{/* Toggle between Login and Signup */}

{isLogin ? "Don't have an account? " : "Already have an account? "}

{/* Footer */}

By continuing, you agree to our{' '} {' '} and{' '}

); }; export default Login;