feat: enhance token management with debug logging for session expiration and immediate access

This commit is contained in:
2025-09-19 19:51:23 +02:00
parent 730044f0b7
commit 0f913ee940

View File

@@ -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,
})