From c8b456bf6a3cc5ee32ec76d605c37fd03d401542 Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 9 Oct 2025 12:56:57 +0300 Subject: [PATCH] fix: handle intentional logout to trigger immediate observer promotion When a user explicitly logs out via the logout button, the session should be removed immediately without grace period, allowing observers to be promoted right away instead of waiting for the grace period to expire. Changes: - Close WebRTC connection immediately on logout - Clear grace period marker for intentional logout detection - Add logging to track logout vs disconnect differentiation This complements the accidental disconnect handling which uses grace period. --- web.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/web.go b/web.go index a12a32a4..ff663e5e 100644 --- a/web.go +++ b/web.go @@ -488,10 +488,30 @@ func handleLogin(c *gin.Context) { } func handleLogout(c *gin.Context) { - // Only clear the cookies for this session, don't invalidate the token + // Get session ID from cookie before clearing + sessionID, _ := c.Cookie("sessionId") + + // Close the WebRTC session immediately for intentional logout + if sessionID != "" { + if session := sessionManager.GetSession(sessionID); session != nil { + websocketLogger.Info(). + Str("sessionID", sessionID). + Msg("Closing session due to intentional logout - no grace period") + + // Close peer connection (will trigger cleanupSession) + if session.peerConnection != nil { + _ = session.peerConnection.Close() + } + + // Clear grace period for intentional logout - observer should be promoted immediately + sessionManager.ClearGracePeriod(sessionID) + } + } + + // Clear the cookies for this session, don't invalidate the token // The token should remain valid for other sessions c.SetCookie("authToken", "", -1, "/", "", false, true) - c.SetCookie("sessionId", "", -1, "/", "", false, true) // Clear session ID cookie too + c.SetCookie("sessionId", "", -1, "/", "", false, true) c.JSON(http.StatusOK, gin.H{"message": "Logout successful"}) }