diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..cb4cb40 --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +# For local development (npm run dev) +VITE_API_URL=http://localhost:8080 \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..0850de5 --- /dev/null +++ b/.env.production @@ -0,0 +1,3 @@ +# For the docker image on the server +# Uses the variable AUTH_HOST from your docker structure [cite: 3] +VITE_API_URL=https://api.andreas-dahm.eu \ No newline at end of file diff --git a/src/pages/LoginPage.jsx b/src/pages/LoginPage.jsx index 3524584..ae245be 100644 --- a/src/pages/LoginPage.jsx +++ b/src/pages/LoginPage.jsx @@ -6,86 +6,130 @@ export default function LoginPage() { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); - const [emailError, setEmailError] = useState(''); - const [passwordError, setPasswordError] = useState(''); + const [feedback, setFeedback] = useState({message: '', isError: false}); + const [isLoading, setIsLoading] = useState(false); - const handleLogin = (e) => { + /** + * Handles the form submission and API call to the backend. + * * @param {Event} e - The form submission event + */ + const handleLogin = async (e) => { e.preventDefault(); + setIsLoading(true); + setFeedback({message: '', isError: false}); - setEmailError(''); - setPasswordError(''); + try { + // Build the API URL using the Vite environment variable + const apiUrl = `${import.meta.env.VITE_API_URL}/api/auth/login`; - let hasError = false; + const response = await fetch(apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({email, password}), + }); - if (!email.includes('@') && !email.includes('.')) { - setEmailError(t('LOGIN.ERROR_EMAIL')); - hasError = true; + // Parse the JSON response (matches our Spring Boot LoginResponse record) + const data = await response.json(); + + if (response.ok) { + // HTTP 200 OK + setFeedback({ + message: `Success: ${data.message} - Welcome ${data.email}!`, + isError: false + }); + + // TODO: Save JWT token or trigger routing here in the future + } else { + // HTTP 401 Unauthorized or other errors + setFeedback({ + message: `Error: ${data.message || 'Login failed'}`, + isError: true + }); + } + } catch (error) { + // Network errors (e.g., backend is not running or CORS issue) + console.error('Login API Error:', error); + setFeedback({ + message: 'Network error: Could not connect to the authentication service.', + isError: true + }); + } finally { + setIsLoading(false); } - - if (password.trim() === '') { - setPasswordError(t('LOGIN.ERROR_PASSWORD')); - hasError = true; - } - - // Stop execution if form inputs are invalid - if (hasError) { - return; - } - - // Proceed with API authentication - console.log('Login attempt with:', email); }; return ( -