import React, { useState } from 'react'; import { Eye, EyeOff } from 'lucide-react'; import logo from '../assets/logo.png'; export default function Auth({ onLoginSuccess, isArabic }) { const [isLogin, setIsLogin] = useState(true); const [isForgot, setIsForgot] = useState(false); const [email, setEmail] = useState(''); const [name, setName] = useState(''); const [password, setPassword] = useState(''); const [token, setToken] = useState(''); const [error, setError] = useState(''); const [message, setMessage] = useState(''); const [loading, setLoading] = useState(false); const [requestSent, setRequestSent] = useState(false); const [showPassword, setShowPassword] = useState(false); const API_URL = "/api/auth"; const handleSubmit = async (e) => { e.preventDefault(); setError(''); setMessage(''); setLoading(true); try { if (isForgot) { // Forgot password flow const res = await fetch(`${API_URL}/forgot-password`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email }), }); const data = await res.json(); if (!res.ok) throw new Error(data.detail || 'Error generating reset link.'); setRequestSent(true); setMessage(isArabic ? 'إن كان البريد مسجلاً فقد أُرسل رمز الاستعادة. أدخله بالأسفل مع كلمة المرور الجديدة.' : 'If the email is registered, a reset token was sent. Enter it below with your new password.'); } else if (isLogin) { // Login flow const res = await fetch(`${API_URL}/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); const data = await res.json(); if (!res.ok) throw new Error(data.detail || 'Login failed.'); localStorage.setItem('qto_token', data.token); localStorage.setItem('qto_user', JSON.stringify(data.user)); onLoginSuccess(data.token, data.user); } else { // Register flow const res = await fetch(`${API_URL}/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password, name }), }); const data = await res.json(); if (!res.ok) throw new Error(data.detail || 'Registration failed.'); setMessage(isArabic ? 'تم التسجيل بنجاح! يمكنك الآن تسجيل الدخول.' : 'Registration successful! You can now log in.'); setIsLogin(true); } } catch (err) { setError(err.message); } finally { setLoading(false); } }; const handleResetPassword = async (e) => { e.preventDefault(); setError(''); setMessage(''); setLoading(true); try { const res = await fetch(`${API_URL}/reset-password`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, password }), }); const data = await res.json(); if (!res.ok) throw new Error(data.detail || 'Reset failed.'); setMessage(isArabic ? 'تم تحديث كلمة المرور بنجاح.' : 'Password updated successfully.'); setIsForgot(false); setIsLogin(true); } catch (err) { setError(err.message); } finally { setLoading(false); } }; return (
{/* Brand Header */}
The QS Hub Logo

THE QS HUB

{isForgot ? (isArabic ? 'استعادة كلمة المرور' : 'Forgot Password') : isLogin ? (isArabic ? 'تسجيل الدخول للمنصة' : 'Sign In to Platform') : (isArabic ? 'إنشاء حساب جديد' : 'Create New Account')}

{error &&
{error}
} {message &&
{message}
} {!isForgot ? (
{!isLogin && (
setName(e.target.value)} placeholder={isArabic ? 'الاسم' : 'John Doe'} />
)}
setEmail(e.target.value)} placeholder="name@company.com" />
setPassword(e.target.value)} placeholder="••••••••" style={{ width: '100%', paddingRight: isArabic ? '10px' : '40px', paddingLeft: isArabic ? '40px' : '10px' }} />
setShowPassword(!showPassword)} style={{ position: 'absolute', top: '50%', transform: 'translateY(-50%)', [isArabic ? 'left' : 'right']: '10px', cursor: 'pointer', color: 'var(--text-muted)' }} > {showPassword ? : }
{isLogin && (
setIsForgot(true)} style={{ fontSize: '0.85rem', color: 'var(--primary)', cursor: 'pointer' }} > {isArabic ? 'نسيت كلمة المرور؟' : 'Forgot Password?'}
)}
) : (
{!requestSent ? (
setEmail(e.target.value)} placeholder="name@company.com" />
) : ( <>
setToken(e.target.value)} placeholder="Reset token" />
setPassword(e.target.value)} placeholder="••••••••" style={{ width: '100%', paddingRight: isArabic ? '10px' : '40px', paddingLeft: isArabic ? '40px' : '10px' }} />
setShowPassword(!showPassword)} style={{ position: 'absolute', top: '50%', transform: 'translateY(-50%)', [isArabic ? 'left' : 'right']: '10px', cursor: 'pointer', color: 'var(--text-muted)' }} > {showPassword ? : }
)}
)}
{isForgot ? ( { setIsForgot(false); setRequestSent(false); setError(''); setMessage(''); }} style={{ color: 'var(--primary)', cursor: 'pointer' }}> {isArabic ? 'العودة لتسجيل الدخول' : 'Back to Login'} ) : isLogin ? ( <> {isArabic ? 'ليس لديك حساب؟ ' : "Don't have an account? "} { setIsLogin(false); setError(''); setMessage(''); }} style={{ color: 'var(--primary)', cursor: 'pointer', fontWeight: 600 }}> {isArabic ? 'سجل الآن' : 'Sign Up'} ) : ( <> {isArabic ? 'لديك حساب بالفعل؟ ' : 'Already have an account? '} { setIsLogin(true); setError(''); setMessage(''); }} style={{ color: 'var(--primary)', cursor: 'pointer', fontWeight: 600 }}> {isArabic ? 'تسجيل دخول' : 'Sign In'} )}
{!isForgot && (
{isArabic ? 'أو الدخول عبر' : 'Or continue with'}
)}
); }