diff --git a/public/city-select.html b/public/city-select.html index 2cf4144..12ce930 100644 --- a/public/city-select.html +++ b/public/city-select.html @@ -265,7 +265,14 @@ } function selectCity(cityName) { - window.location.href = `/city/${cityName}`; + const urlParams = new URLSearchParams(window.location.search); + const fromDashboard = urlParams.get('from') === 'dashboard'; + + if (user || fromDashboard) { + window.location.href = `/city/${cityName}/dashboard`; + } else { + window.location.href = `/city/${cityName}`; + } } function showMessage(message, type = 'success') { diff --git a/public/dashboard.html b/public/dashboard.html index 9210f36..922363d 100644 --- a/public/dashboard.html +++ b/public/dashboard.html @@ -322,7 +322,7 @@

💧 Water Stations - Loading...

Welcome, - Change City + Change City diff --git a/public/index.html b/public/index.html index 5061a94..9dd0c35 100644 --- a/public/index.html +++ b/public/index.html @@ -133,8 +133,10 @@

Loading...

- Login - All Cities +
+ Login + All Cities +
@@ -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); diff --git a/public/login.html b/public/login.html index 51b8935..491bccf 100644 --- a/public/login.html +++ b/public/login.html @@ -211,8 +211,8 @@ or - Google - Instagram + Google + Instagram
Don't have an account? Register @@ -240,8 +240,8 @@ or
- Sign up with Google - Sign up with Instagram + Sign up with Google + Sign up with Instagram
Already have an account? Login @@ -292,7 +292,18 @@ }); if (response.ok) { - window.location.href = '/dashboard'; + const urlParams = new URLSearchParams(window.location.search); + const redirectUrl = urlParams.get('redirect'); + if (redirectUrl) { + const cityMatch = redirectUrl.match(/\/city\/([^\/]+)/); + if (cityMatch) { + window.location.href = `/city/${cityMatch[1]}/dashboard`; + } else { + window.location.href = '/dashboard'; + } + } else { + window.location.href = '/dashboard'; + } } else { showMessage('Invalid username or password'); } @@ -321,7 +332,18 @@ if (response.ok) { showMessage('Registration successful! Redirecting...', 'success'); setTimeout(() => { - window.location.href = '/dashboard'; + const urlParams = new URLSearchParams(window.location.search); + const redirectUrl = urlParams.get('redirect'); + if (redirectUrl) { + const cityMatch = redirectUrl.match(/\/city\/([^\/]+)/); + if (cityMatch) { + window.location.href = `/city/${cityMatch[1]}/dashboard`; + } else { + window.location.href = '/dashboard'; + } + } else { + window.location.href = '/dashboard'; + } }, 1500); } else { showMessage(result.error || 'Registration failed'); @@ -330,6 +352,20 @@ showMessage('Registration failed. Please try again.'); } }); + + // Update OAuth links with redirect parameter + document.addEventListener('DOMContentLoaded', function() { + const urlParams = new URLSearchParams(window.location.search); + const redirectUrl = urlParams.get('redirect'); + + if (redirectUrl) { + const encodedRedirect = encodeURIComponent(redirectUrl); + document.getElementById('googleBtn').href = `/auth/google?redirect=${encodedRedirect}`; + document.getElementById('instagramBtn').href = `/auth/instagram?redirect=${encodedRedirect}`; + document.getElementById('googleRegBtn').href = `/auth/google?redirect=${encodedRedirect}`; + document.getElementById('instagramRegBtn').href = `/auth/instagram?redirect=${encodedRedirect}`; + } + }); \ No newline at end of file diff --git a/server.js b/server.js index c857cdc..0e54df3 100644 --- a/server.js +++ b/server.js @@ -185,25 +185,35 @@ app.get('/city-select', (req, res) => { }); // Authentication routes -app.get('/auth/google', - passport.authenticate('google', { scope: ['profile', 'email'] }) -); +app.get('/auth/google', (req, res, next) => { + if (req.query.redirect) { + req.session.redirectUrl = req.query.redirect; + } + passport.authenticate('google', { scope: ['profile', 'email'] })(req, res, next); +}); app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), (req, res) => { - res.redirect('/city/salem/dashboard'); + const redirectUrl = req.session.redirectUrl || '/city/salem/dashboard'; + delete req.session.redirectUrl; + res.redirect(redirectUrl); } ); -app.get('/auth/instagram', - passport.authenticate('instagram') -); +app.get('/auth/instagram', (req, res, next) => { + if (req.query.redirect) { + req.session.redirectUrl = req.query.redirect; + } + passport.authenticate('instagram')(req, res, next); +}); app.get('/auth/instagram/callback', passport.authenticate('instagram', { failureRedirect: '/login' }), (req, res) => { - res.redirect('/city/salem/dashboard'); + const redirectUrl = req.session.redirectUrl || '/city/salem/dashboard'; + delete req.session.redirectUrl; + res.redirect(redirectUrl); } ); @@ -420,6 +430,9 @@ app.get('/city/:cityName/dashboard', (req, res) => { }); app.get('/login', (req, res) => { + if (req.query.redirect) { + req.session.redirectUrl = req.query.redirect; + } res.sendFile(path.join(__dirname, 'public', 'login.html')); });