refactor: de-slop the server and refactor login/logout
This commit is contained in:
parent
5235a12513
commit
29f3207840
@ -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 {
|
||||
|
||||
10
src/App.tsx
10
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 (
|
||||
<div className="min-h-screen flex items-center justify-center">
|
||||
<div className="text-xl">Loading...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
const { user, handleLogin, handleLogout } = useAuth();
|
||||
|
||||
return (
|
||||
<div className="min-h-screen py-4 px-4 sm:py-8 sm:px-6 lg:px-8">
|
||||
|
||||
@ -4,44 +4,22 @@ import { authService } from '../services/api';
|
||||
|
||||
export const useAuth = () => {
|
||||
const [user, setUser] = useState<SteamUser | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
// Check if user is already authenticated on mount
|
||||
useEffect(() => {
|
||||
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);
|
||||
console.error('Error checking auth status:', error);
|
||||
// User is not authenticated, which is fine
|
||||
}
|
||||
};
|
||||
|
||||
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 handleLogin = () => {
|
||||
@ -60,5 +38,5 @@ export const useAuth = () => {
|
||||
}
|
||||
};
|
||||
|
||||
return { user, loading, handleLogin, handleLogout };
|
||||
return { user, handleLogin, handleLogout };
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user