This commit is contained in:
Mateusz Gruszczyński
2025-07-06 10:01:04 +02:00
parent 879a5e073d
commit 5ea203cbe1
4 changed files with 53 additions and 40 deletions

16
app.py
View File

@ -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:

View File

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

View File

@ -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');
});

View File

@ -71,6 +71,7 @@
{% if request.endpoint != 'system_auth' %}
<script src="{{ url_for('static_bp.serve_js', filename='live.js') }}"></script>
<script src="{{ url_for('static_bp.serve_js', filename='hide_list.js') }}"></script>
<script src="{{ url_for('static_bp.serve_js', filename='socket_reconnect.js') }}"></script>
{% endif %}
<script src="{{ url_for('static_bp.serve_js', filename='toasts.js') }}"></script>
{% block scripts %}{% endblock %}