Spaces:
Running
Running
File size: 8,727 Bytes
1a19944 b7dce4c 98d3405 1a19944 b7dce4c 1a19944 e4735a0 d62d909 1a19944 98d3405 1a19944 b7dce4c 1a19944 98d3405 1a19944 98d3405 1a19944 b7dce4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | import React, { useState } from 'react';
import { Eye, EyeOff, Mail, Lock, ArrowRight, BookOpen, Phone } from 'lucide-react';
import { useAppConfig } from '../contexts/AppConfigContext';
import CopyrightNotice from './CopyrightNotice';
import '../styles/Login.css';
const Login = ({ onNavigateToSignup, onNavigateToHome }) => {
const { config } = useAppConfig();
const [showPassword, setShowPassword] = useState(false);
const [formData, setFormData] = useState({
email: '',
password: ''
});
const [isLoading, setIsLoading] = useState(false);
const [errors, setErrors] = useState({});
const handleInputChange = (e) => {
const { name, value } = e.target;
setFormData(prev => ({
...prev,
[name]: value
}));
// Clear error when user starts typing
if (errors[name]) {
setErrors(prev => ({
...prev,
[name]: ''
}));
}
};
const validateForm = () => {
const newErrors = {};
if (!formData.email) {
newErrors.email = 'Email is required';
} else if (!/\S+@\S+\.\S+/.test(formData.email)) {
newErrors.email = 'Please enter a valid email address';
}
if (!formData.password) {
newErrors.password = 'Password is required';
} else if (formData.password.length < 6) {
newErrors.password = 'Password must be at least 6 characters';
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSubmit = async (e) => {
e.preventDefault();
if (!validateForm()) return;
setIsLoading(true);
try {
const response = await fetch(`${process.env.REACT_APP_API_URL}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: formData.email,
password: formData.password
}),
});
const data = await response.json();
if (response.ok) {
// Store the token and user info
localStorage.setItem('authToken', data.access_token);
localStorage.setItem('user', JSON.stringify(data.user));
onNavigateToHome?.(data.user, data.access_token);
} else {
setErrors({ submit: data.detail || 'Login failed. Please try again.' });
}
} catch (error) {
console.error('Login error:', error);
setErrors({ submit: 'Login failed. Please try again.' });
} finally {
setIsLoading(false);
}
};
const handleGoogleSignIn = () => {
console.log('Google Sign In clicked');
// Future Google Auth integration will go here
};
const handlePhoneSignIn = () => {
console.log('Phone Sign In clicked');
// Future Phone Auth integration will go here
};
return (
<div className="login-page">
<div className="login-content">
<div className="login-container">
{/* Header */}
<div className="login-header">
<div className="logo-container">
<BookOpen className="logo-icon" />
</div>
<h1 className="login-title">Welcome Back</h1>
<p className="login-subtitle">
{config?.login?.subtitle || 'Sign in to continue'}
</p>
</div>
{/* Main Login Form */}
<div className="login-form-container">
<form onSubmit={handleSubmit} className="login-form">
{/* Email Field */}
<div className="form-group">
<label htmlFor="email" className="form-label">
Email Address
</label>
<div className="input-container">
<Mail className="input-icon" />
<input
type="email"
id="email"
name="email"
value={formData.email}
onChange={handleInputChange}
className={`form-input ${errors.email ? 'error' : ''}`}
placeholder="Enter your email"
disabled={isLoading}
/>
</div>
{errors.email && (
<span className="error-message">{errors.email}</span>
)}
</div>
{/* Password Field */}
<div className="form-group">
<label htmlFor="password" className="form-label">
Password
</label>
<div className="input-container">
<Lock className="input-icon" />
<input
type={showPassword ? 'text' : 'password'}
id="password"
name="password"
value={formData.password}
onChange={handleInputChange}
className={`form-input ${errors.password ? 'error' : ''}`}
placeholder="Enter your password"
disabled={isLoading}
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="password-toggle"
disabled={isLoading}
>
{showPassword ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
</div>
{errors.password && (
<span className="error-message">{errors.password}</span>
)}
</div>
{/* Forgot Password */}
<div className="form-actions">
<button type="button" className="forgot-password">
Forgot your password?
</button>
</div>
{/* Submit Error */}
{errors.submit && (
<div className="submit-error">
{errors.submit}
</div>
)}
{/* Submit Button */}
<button
type="submit"
className={`submit-btn ${isLoading ? 'loading' : ''}`}
disabled={isLoading}
>
{isLoading ? (
<>
<div className="loading-spinner"></div>
Signing In...
</>
) : (
<>
Sign In
<ArrowRight size={16} />
</>
)}
</button>
</form>
{/* Divider */}
<div className="divider">
<span>or continue with</span>
</div>
{/* Social Login Options */}
<div className="social-login">
<button
type="button"
className="google-btn"
onClick={handleGoogleSignIn}
disabled={isLoading}
>
<svg className="google-icon" viewBox="0 0 24 24">
<path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/>
<path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/>
<path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/>
<path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/>
</svg>
Continue with Google
</button>
<button
type="button"
className="phone-btn"
onClick={handlePhoneSignIn}
disabled={isLoading}
>
<Phone size={16} />
Continue with Phone
</button>
</div>
</div>
{/* Footer */}
<div className="login-footer">
<p>
Don't have an account?{' '}
<button
type="button"
className="link-btn"
onClick={onNavigateToSignup}
>
Sign up here
</button>
</p>
<div className="login-powered-by">
<a href="https://neon.ai" target="_blank" rel="noopener noreferrer" className="footer-neon-link">
<img src="/neon-logo.png" alt="" className="footer-neon-logo" />
Powered by Neon.ai
</a>
</div>
</div>
</div>
</div>
<footer className="login-page-footer">
<CopyrightNotice className="login-page-copyright" />
</footer>
</div>
);
};
export default Login;
|