diff --git a/app/api/socketio/route.js b/app/api/socketio/route.js index 116b356..bf81a34 100644 --- a/app/api/socketio/route.js +++ b/app/api/socketio/route.js @@ -69,6 +69,8 @@ async function evaluateQueue(eventId, io) { expiresIn: TOKEN_TTL_SECONDS, }) + console.log(`Creating token for ${next.substring(0, 8)}: expires at ${expiresAt.toISOString()}, TTL: ${TOKEN_TTL_SECONDS}s`) + ev.active.add(next) // Save to database @@ -89,14 +91,32 @@ async function evaluateQueue(eventId, io) { // Check for expired tokens in database and notify clients if (connection) { try { + // Debug: check current time vs stored times + const [allSessions] = await connection.execute( + 'SELECT socket_id, expires_at, NOW() as current_time FROM active_sessions WHERE event_id = ?', + [eventId] + ) + + console.log(`Event ${eventId} - Current sessions:`, allSessions.map(s => ({ + socket: s.socket_id.substring(0, 8), + expires: s.expires_at, + current: s.current_time, + expired: new Date(s.expires_at) < new Date(s.current_time) + }))) + const [expiredSessions] = await connection.execute( 'SELECT socket_id FROM active_sessions WHERE event_id = ? AND expires_at < NOW()', [eventId] ) + if (expiredSessions.length > 0) { + console.log(`Found ${expiredSessions.length} expired sessions for event ${eventId}`) + } + for (const session of expiredSessions) { const sid = session.socket_id if (ev.active.has(sid)) { + console.log(`Sending token_expired to ${sid.substring(0, 8)}`) ev.active.delete(sid) io.to(sid).emit("token_expired") } @@ -254,8 +274,10 @@ export async function GET(req) { // If queue is NOT active and user doesn't have access, grant it immediately if (!ev.queueOn && !ev.active.has(socket.id)) { - console.log(`Granting immediate access to ${socket.id} (under threshold)`) + console.log(`Granting immediate access to ${socket.id.substring(0, 8)} (under threshold)`) const expiresAt = new Date(Date.now() + TOKEN_TTL_SECONDS * 1000) + console.log(`Creating immediate token: expires at ${expiresAt.toISOString()}, TTL: ${TOKEN_TTL_SECONDS}s`) + const token = jwt.sign({ sid: socket.id, eventId }, process.env.JWT_SECRET || "dev-secret", { expiresIn: TOKEN_TTL_SECONDS, })