refactor: de-slop the server and refactor login/logout

This commit is contained in:
ethanf 2025-08-14 15:24:18 -05:00
parent 5235a12513
commit 29f3207840
3 changed files with 19 additions and 100 deletions

View File

@ -71,18 +71,6 @@ app.use(session({
app.use(passport.initialize()); app.use(passport.initialize());
app.use(passport.session()); 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 // Serve static files from React build in production
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../dist'))); app.use(express.static(path.join(__dirname, '../dist')));
@ -117,50 +105,18 @@ passport.deserializeUser((user, done) => {
done(null, user); 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 // Routes
app.get('/auth/steam', passport.authenticate('steam')); app.get('/auth/steam', passport.authenticate('steam'));
app.get('/auth/steam/return', app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }), passport.authenticate('steam', { failureRedirect: '/' }),
(req, res) => { (req, res) => {
console.log('=== Steam Auth Callback ==='); console.log('User authenticated:', req.user);
console.log('Session ID after auth:', req.sessionID); res.redirect(FRONTEND_URL);
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);
});
} }
); );
app.get('/auth/logout', (req, res) => { app.post('/auth/logout', (req, res) => {
req.logout((err) => { req.logout((err) => {
if (err) { if (err) {
return res.status(500).json({ error: 'Logout failed' }); return res.status(500).json({ error: 'Logout failed' });
@ -170,13 +126,6 @@ app.get('/auth/logout', (req, res) => {
}); });
app.get('/auth/user', (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()) { if (req.isAuthenticated()) {
res.json({ user: req.user }); res.json({ user: req.user });
} else { } else {

View File

@ -6,15 +6,7 @@ import VotingInterface from "./components/VotingInterface";
import "./App.css"; import "./App.css";
function App() { function App() {
const { user, loading, handleLogin, handleLogout } = useAuth(); const { user, handleLogin, handleLogout } = useAuth();
if (loading) {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="text-xl">Loading...</div>
</div>
);
}
return ( return (
<div className="min-h-screen py-4 px-4 sm:py-8 sm:px-6 lg:px-8"> <div className="min-h-screen py-4 px-4 sm:py-8 sm:px-6 lg:px-8">

View File

@ -4,44 +4,22 @@ import { authService } from '../services/api';
export const useAuth = () => { export const useAuth = () => {
const [user, setUser] = useState<SteamUser | null>(null); const [user, setUser] = useState<SteamUser | null>(null);
const [loading, setLoading] = useState(true);
// Check if user is already authenticated on mount
useEffect(() => {
const checkAuthStatus = async () => { const checkAuthStatus = async () => {
try { try {
console.log('Checking authentication status...');
const response = await authService.getCurrentUser(); const response = await authService.getCurrentUser();
console.log('Auth check response:', response);
if (response.user) { if (response.user) {
setUser(response.user); setUser(response.user);
console.log('User set successfully:', response.user);
} else {
setUser(null);
console.log('No user found in response');
} }
} catch (error) { } catch (error) {
console.log('User not authenticated:', error); console.error('Error checking auth status:', error);
setUser(null); // User is not authenticated, which is fine
} finally {
setLoading(false);
} }
}; };
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(); checkAuthStatus();
}, 2000); // Increased delay to 2 seconds
} else {
checkAuthStatus();
}
}, []); }, []);
const handleLogin = () => { const handleLogin = () => {
@ -60,5 +38,5 @@ export const useAuth = () => {
} }
}; };
return { user, loading, handleLogin, handleLogout }; return { user, handleLogin, handleLogout };
}; };