Fix login flows
This commit is contained in:
@@ -133,8 +133,10 @@
|
||||
<p id="cityName">Loading...</p>
|
||||
</div>
|
||||
|
||||
<a href="/login" class="auth-button">Login</a>
|
||||
<a href="/city-select" class="auth-button" style="top: 1rem; right: 6rem;">All Cities</a>
|
||||
<div id="authButtons">
|
||||
<a href="/login" class="auth-button" id="loginBtn">Login</a>
|
||||
<a href="/city-select" class="auth-button" style="top: 1rem; right: 6rem;" id="changeCityBtn">All Cities</a>
|
||||
</div>
|
||||
|
||||
<div id="map"></div>
|
||||
|
||||
@@ -163,6 +165,36 @@
|
||||
let map;
|
||||
let stations = [];
|
||||
let currentCity = null;
|
||||
let user = null;
|
||||
|
||||
async function checkAuth() {
|
||||
try {
|
||||
const response = await fetch('/api/user');
|
||||
const data = await response.json();
|
||||
user = data.user;
|
||||
updateAuthButtons();
|
||||
} catch (error) {
|
||||
console.error('Auth check failed:', error);
|
||||
updateAuthButtons();
|
||||
}
|
||||
}
|
||||
|
||||
function updateAuthButtons() {
|
||||
const loginBtn = document.getElementById('loginBtn');
|
||||
const changeCityBtn = document.getElementById('changeCityBtn');
|
||||
|
||||
if (user) {
|
||||
loginBtn.textContent = 'Dashboard';
|
||||
loginBtn.href = `/city/${currentCity}/dashboard`;
|
||||
changeCityBtn.textContent = 'Change City';
|
||||
changeCityBtn.href = '/city-select';
|
||||
} else {
|
||||
loginBtn.textContent = 'Login';
|
||||
loginBtn.href = `/login?redirect=${encodeURIComponent(window.location.pathname)}`;
|
||||
changeCityBtn.textContent = 'All Cities';
|
||||
changeCityBtn.href = '/city-select';
|
||||
}
|
||||
}
|
||||
|
||||
function initMap() {
|
||||
// Get city name from URL
|
||||
@@ -184,6 +216,7 @@
|
||||
attribution: '© OpenStreetMap contributors'
|
||||
}).addTo(map);
|
||||
|
||||
checkAuth();
|
||||
loadStations();
|
||||
}
|
||||
|
||||
@@ -197,6 +230,7 @@
|
||||
}
|
||||
displayStations();
|
||||
fitMapToStations();
|
||||
updateAuthButtons();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error loading stations:', error);
|
||||
|
||||
Reference in New Issue
Block a user