// frontend/js/auth.js // --- CONFIGURATION --- const SOCKET_URL = window.location.origin; // Connects to the server that served the page const API_BASE = "/api"; // Relative path for API calls document.addEventListener('DOMContentLoaded', () => { const loginForm = document.getElementById('login-form'); const errorMessageDiv = document.getElementById('error-message'); loginForm.addEventListener('submit', async (event) => { event.preventDefault(); errorMessageDiv.classList.add('hidden'); const username = document.getElementById('username').value; const password = document.getElementById('password').value; try { const response = await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }), }); const data = await response.json(); if (response.ok) { // Save user to localStorage and go to station selection localStorage.setItem('user', JSON.stringify(data.user)); window.location.href = 'station_selection.html'; } else { errorMessageDiv.textContent = data.message || 'Login failed.'; errorMessageDiv.classList.remove('hidden'); } } catch (error) { errorMessageDiv.textContent = 'Failed to connect to the server.'; errorMessageDiv.classList.remove('hidden'); console.error('Login error:', error); } }); });