import React, { useState } from 'react'; import { TextField, Button, Typography, Stack, Box, IconButton, InputAdornment } from '@mui/material'; import { useTheme } from '@mui/material/styles'; import VisibilityOffOutlinedIcon from '@mui/icons-material/VisibilityOffOutlined'; import { VisibilityOutlined } from '@mui/icons-material'; import SidePanel from './SidePanel'; import { useNavigate, Link } from 'react-router-dom'; import authService from '../../../services/authService'; import { GoogleLogin, useGoogleLogin } from '@react-oauth/google'; import { jwtDecode } from "jwt-decode"; const LoginForm = () => { const navigate = useNavigate(); const theme = useTheme(); const [showPassword, setShowPassword] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const handleTogglePassword = () => { setShowPassword(prev => !prev); }; const handleLogin = async () => { setLoading(true); setError(''); const result = await authService.login(email, password); if (!result.success) { // 🔹 أولًا: نتحقق إن كانت الرسالة تشير إلى أن الحساب غير موجود if (result.message && result.message.includes('Account not found')) { setError('This account is not registered.'); } // 🔹 ثانيًا: نتحقق من "Invalid credentials" else if (result.message && result.message.includes('Invalid credentials')) { setError('Email or password is incorrect.'); } // 🔹 ثالثًا: نعرض أول خطأ موجود في كائن errors else if (result.errors) { const firstError = Object.values(result.errors)[0][0]; setError(firstError); } // 🔹 أخيرًا: fallback لأي رسالة أخرى else { setError(result.message || 'Login failed'); } setLoading(false); return; } localStorage.setItem('token', result.data.token); localStorage.setItem('refresh_token', result.data.refresh_token); setLoading(false); navigate('/restaurant', { replace: true }); }; const handleGoogleSuccess = async (tokenResponse) => { try { const res = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', { headers: { Authorization: `Bearer ${tokenResponse.access_token}`, }, }); const userInfo = await res.json(); console.log('Google User Info:', userInfo); if (userInfo.email) { setEmail(userInfo.email); } setPassword(''); localStorage.setItem("google_user", JSON.stringify(userInfo)); } catch (e) { console.error("Google login failed:", e); setError("Google login failed"); } }; const handleGoogleError = () => { setError("Google login failed"); }; const loginWithGoogle = useGoogleLogin({ onSuccess: handleGoogleSuccess, onError: handleGoogleError, }); return ( {/* الشعار */} logo {/* عنوان تسجيل الدخول */} Login Enter your username and password to access your account securely. Welcome back to our service! {/* إدخال البريد */} Email setEmail(e.target.value)} sx={{ '& input': { fontWeight: 500, fontSize: '15px' }, '& input::placeholder': { color: '#969BA7' }, '& .MuiOutlinedInput-root': { borderRadius: '10px', transition: '0.3s', '&.Mui-focused fieldset': { borderColor: theme.palette.primary.main, boxShadow: '0 0 0 2px rgba(255, 145, 77, 0.2)' } }, '& .MuiOutlinedInput-root.Mui-focused': { borderColor: '#3f51b5', boxShadow: '0 0 0 2px rgba(63,81,181,0.1)' } }} /> {/* إدخال كلمة السر */} Password setPassword(e.target.value)} sx={{ '& input': { fontWeight: 500, fontSize: '15px' }, '& input::placeholder': { color: '#969BA7' }, '& .MuiOutlinedInput-root': { borderRadius: '10px', transition: '0.3s', '&.Mui-focused fieldset': { borderColor: theme.palette.primary.main, boxShadow: '0 0 0 2px rgba(255, 145, 77, 0.2)' } }, '& .MuiOutlinedInput-root.Mui-focused': { borderColor: '#3f51b5', boxShadow: '0 0 0 2px rgba(63,81,181,0.1)' }, '& input::-ms-reveal, & input::-ms-clear': { display: 'none', }, }} InputProps={{ endAdornment: ( {showPassword ? : } ) }} /> {/* عرض الخطأ */} {error && ( {error} )} {/* زر تسجيل الدخول */} {/* Divider */} Or {/* */} {/* Facebook Button */} {/* */} {/* Register Link */} Don’t have an account?{' '} Register {/* ForgetPassword Link */} Did you forget the password?{' '} Forget Password {/* Terms */} By logging in, I agree to the{' '} window.open('/terms', 'TermsOfService')} > Terms of Service {' '} and{' '} window.open('/privacy', 'PrivacyPolicy')} > Privacy Policy . ); }; export default LoginForm;