diff --git a/app.py b/app.py index ad09bab..63af4a1 100644 --- a/app.py +++ b/app.py @@ -104,7 +104,7 @@ def serve_js(filename): response.cache_control.no_cache = True response.cache_control.no_store = True response.cache_control.must_revalidate = True - response.expires = 0 + #response.expires = 0 response.pragma = 'no-cache' response.headers.pop('Content-Disposition', None) response.headers.pop('Etag', None) @@ -183,9 +183,21 @@ def inject_has_authorized_cookie(): def require_system_password(): if 'authorized' not in request.cookies \ and request.endpoint != 'system_auth' \ - and not request.endpoint.startswith('static') \ and not request.endpoint.startswith('login') \ and request.endpoint != 'favicon': + # specjalny wyjątek dla statycznych, ale sprawdzany ręcznie niżej + if request.endpoint == 'static_bp.serve_js': + # tu sprawdzamy czy to JS, który ma być chroniony + protected_js = ["live.js", "list_guest.js", "hide_list.js", "socket_reconnect.js"] + requested_file = request.view_args.get("filename", "") + if requested_file in protected_js: + return redirect(url_for('system_auth', next=request.url)) + else: + return # pozwól na inne pliki statyczne + + if request.endpoint.startswith('static_bp.'): + return # np. CSS, favicon, inne — pozwól + if request.path == '/': return redirect(url_for('system_auth')) else: diff --git a/static/js/live.js b/static/js/live.js index e8b655c..b5c0ee4 100644 --- a/static/js/live.js +++ b/static/js/live.js @@ -1,43 +1,5 @@ const socket = io(); - -// --- Automatyczny reconnect po powrocie do karty/przywróceniu internetu --- -function reconnectIfNeeded() { - if (!socket.connected) { - socket.connect(); - } -} - -document.addEventListener("visibilitychange", function() { - if (!document.hidden) { - reconnectIfNeeded(); - } -}); - -window.addEventListener("focus", function() { - reconnectIfNeeded(); -}); - -window.addEventListener("online", function() { - reconnectIfNeeded(); -}); - -// --- Toasty przy rozłączeniu i połączeniu --- -let firstConnect = true; - -socket.on('connect', function() { - if (!firstConnect) { - showToast('Połączono z serwerem! 🔄', 'info'); - } - firstConnect = false; -}); - -socket.on('disconnect', function(reason) { - showToast('Utracono połączenie z serwerem...', 'warning'); -}); - -// --- koniec fragmentu reconnect --- - function setupList(listId, username) { socket.emit('join_list', { room: listId, username: username }); diff --git a/static/js/socket_reconnect.js b/static/js/socket_reconnect.js new file mode 100644 index 0000000..c4ea4b4 --- /dev/null +++ b/static/js/socket_reconnect.js @@ -0,0 +1,38 @@ +// --- Automatyczny reconnect po powrocie do karty/przywróceniu internetu --- +function reconnectIfNeeded() { + if (!socket.connected) { + socket.connect(); + } +} + +document.addEventListener("visibilitychange", function() { + if (!document.hidden) { + reconnectIfNeeded(); + } +}); + +window.addEventListener("focus", function() { + reconnectIfNeeded(); +}); + +window.addEventListener("online", function() { + reconnectIfNeeded(); +}); + +// --- Toasty przy rozłączeniu i połączeniu --- +let firstConnect = true; + +socket.on('connect', function() { + if (!firstConnect) { + showToast('Połączono z serwerem! 🔄', 'info'); + // Automatyczne ponowne dołączenie do pokoju + if (window.LIST_ID && window.usernameForReconnect) { + socket.emit('join_list', { room: window.LIST_ID, username: window.usernameForReconnect }); + } + } + firstConnect = false; +}); + +socket.on('disconnect', function(reason) { + showToast('Utracono połączenie z serwerem...', 'warning'); +}); diff --git a/templates/base.html b/templates/base.html index cec583b..f6b4e1d 100644 --- a/templates/base.html +++ b/templates/base.html @@ -71,6 +71,7 @@ {% if request.endpoint != 'system_auth' %} + {% endif %} {% block scripts %}{% endblock %}