From 29f3207840ff86f3edc3a74962c776f0df64e7c7 Mon Sep 17 00:00:00 2001 From: ethanf Date: Thu, 14 Aug 2025 15:24:18 -0500 Subject: [PATCH] refactor: de-slop the server and refactor login/logout --- server/server.js | 57 +++----------------------------------------- src/App.tsx | 10 +------- src/hooks/useAuth.ts | 52 ++++++++++++---------------------------- 3 files changed, 19 insertions(+), 100 deletions(-) diff --git a/server/server.js b/server/server.js index a7c7835..a4122bd 100644 --- a/server/server.js +++ b/server/server.js @@ -71,18 +71,6 @@ app.use(session({ app.use(passport.initialize()); app.use(passport.session()); -// Add debugging middleware to track sessions -app.use((req, res, next) => { - console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`); - console.log('Session ID:', req.sessionID); - console.log('Session data:', req.session); - console.log('User authenticated:', req.isAuthenticated()); - console.log('User data:', req.user); - console.log('Cookie header:', req.headers.cookie); - console.log('---'); - next(); -}); - // Serve static files from React build in production if (process.env.NODE_ENV === 'production') { app.use(express.static(path.join(__dirname, '../dist'))); @@ -117,50 +105,18 @@ passport.deserializeUser((user, done) => { done(null, user); }); -// Session debug endpoint -app.get('/debug/session', (req, res) => { - res.json({ - sessionID: req.sessionID, - isAuthenticated: req.isAuthenticated(), - user: req.user, - session: req.session, - cookies: req.headers.cookie, - secure: req.secure, - protocol: req.protocol, - host: req.get('host') - }); -}); - // Routes app.get('/auth/steam', passport.authenticate('steam')); app.get('/auth/steam/return', passport.authenticate('steam', { failureRedirect: '/' }), (req, res) => { - console.log('=== Steam Auth Callback ==='); - console.log('Session ID after auth:', req.sessionID); - console.log('User after auth:', req.user); - console.log('Is authenticated after auth:', req.isAuthenticated()); - - // Force session save before redirect - req.session.save((err) => { - if (err) { - console.error('Session save error:', err); - return res.redirect('/?auth=error'); - } - - console.log('Session saved successfully'); - console.log('Session after save:', req.session); - - // Redirect to frontend with a small delay to ensure session is saved - setTimeout(() => { - res.redirect('/?auth=success'); - }, 100); - }); + console.log('User authenticated:', req.user); + res.redirect(FRONTEND_URL); } ); -app.get('/auth/logout', (req, res) => { +app.post('/auth/logout', (req, res) => { req.logout((err) => { if (err) { return res.status(500).json({ error: 'Logout failed' }); @@ -170,13 +126,6 @@ app.get('/auth/logout', (req, res) => { }); app.get('/auth/user', (req, res) => { - console.log('=== /auth/user endpoint ==='); - console.log('Session ID:', req.sessionID); - console.log('Session:', req.session); - console.log('User:', req.user); - console.log('Is authenticated:', req.isAuthenticated()); - console.log('========================'); - if (req.isAuthenticated()) { res.json({ user: req.user }); } else { diff --git a/src/App.tsx b/src/App.tsx index 06b1115..a92dcf6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,15 +6,7 @@ import VotingInterface from "./components/VotingInterface"; import "./App.css"; function App() { - const { user, loading, handleLogin, handleLogout } = useAuth(); - - if (loading) { - return ( -
-
Loading...
-
- ); - } + const { user, handleLogin, handleLogout } = useAuth(); return (
diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index e76f191..34ae690 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -4,44 +4,22 @@ import { authService } from '../services/api'; export const useAuth = () => { const [user, setUser] = useState(null); - const [loading, setLoading] = useState(true); - - const checkAuthStatus = async () => { - try { - console.log('Checking authentication status...'); - const response = await authService.getCurrentUser(); - console.log('Auth check response:', response); - - if (response.user) { - setUser(response.user); - console.log('User set successfully:', response.user); - } else { - setUser(null); - console.log('No user found in response'); - } - } catch (error) { - console.log('User not authenticated:', error); - setUser(null); - } finally { - setLoading(false); - } - }; + // Check if user is already authenticated on mount useEffect(() => { - // Check for auth success parameter first - const urlParams = new URLSearchParams(window.location.search); - if (urlParams.get('auth') === 'success') { - console.log('Auth success detected, waiting before checking user status...'); - // Remove the auth parameter from URL - window.history.replaceState({}, document.title, window.location.pathname); - - // Wait a bit longer for session to be fully established - setTimeout(() => { - checkAuthStatus(); - }, 2000); // Increased delay to 2 seconds - } else { - checkAuthStatus(); - } + const checkAuthStatus = async () => { + try { + const response = await authService.getCurrentUser(); + if (response.user) { + setUser(response.user); + } + } catch (error) { + console.error('Error checking auth status:', error); + // User is not authenticated, which is fine + } + }; + + checkAuthStatus(); }, []); const handleLogin = () => { @@ -60,5 +38,5 @@ export const useAuth = () => { } }; - return { user, loading, handleLogin, handleLogout }; + return { user, handleLogin, handleLogout }; }; \ No newline at end of file