Fix Keycloak login: force fresh WebSocket before auth.keycloak
The server closes idle unauthenticated connections after AuthTimeoutMs (10 s). A user authenticating in the browser takes longer than that, so the socket the client held while the browser was open is already dead when token_received fires. Sending auth.keycloak on that stale connection produces no reply and the scene never changes. Fix: login_with_keycloak() now calls Network.force_reconnect() instead of sending immediately. _on_network_connected() then sends auth.keycloak as the guaranteed-first message on the fresh connection. Also: DISCONNECTED errors in the auth.keycloak error callback no longer clear keycloak_refresh_token, preserving silent re-auth on transient drops. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -79,9 +79,13 @@ func login_with_keycloak(access_token: String, refresh_token: String) -> void:
|
||||
keycloak_refresh_token = refresh_token
|
||||
_save_session()
|
||||
_pending_keycloak_token = access_token
|
||||
if Network.is_online():
|
||||
_send_keycloak_auth()
|
||||
# else: _on_network_connected will send it once the socket connects.
|
||||
# Always force a fresh WebSocket connection so auth.keycloak is the very
|
||||
# first message the server sees. The socket that was open while the user
|
||||
# was in the browser has likely already been closed by the server's
|
||||
# AuthTimeoutMs (10 s of silence) — sending on that stale connection
|
||||
# yields no reply. force_reconnect() closes it immediately; the next
|
||||
# _on_network_connected() will call _send_keycloak_auth().
|
||||
Network.force_reconnect()
|
||||
|
||||
|
||||
## Clear all auth state and disconnect, returning to the login screen.
|
||||
@@ -146,6 +150,11 @@ func _send_keycloak_auth() -> void:
|
||||
{ "keycloakToken": token },
|
||||
func(_type: String, p: Variant): _on_auth_ok(p),
|
||||
func(code: String, msg: String):
|
||||
if code == "DISCONNECTED":
|
||||
# Transient drop — keep the refresh token and surface the error.
|
||||
# _on_network_connected will retry auth.keycloak on the next reconnect.
|
||||
auth_failed.emit(code, msg)
|
||||
return
|
||||
# Keycloak token rejected (invalid, expired, missing role).
|
||||
keycloak_refresh_token = ""
|
||||
_save_session()
|
||||
|
||||
@@ -243,7 +243,7 @@ func _exchange_code(code: String) -> void:
|
||||
func _on_exchange_completed(
|
||||
_result: int, status: int,
|
||||
_headers: PackedStringArray, body: PackedByteArray) -> void:
|
||||
var json := JSON.parse_string(body.get_string_from_utf8())
|
||||
var json : Variant = JSON.parse_string(body.get_string_from_utf8())
|
||||
if status != 200 or json == null or not (json is Dictionary):
|
||||
auth_error.emit("Token exchange failed (HTTP %d)." % status)
|
||||
return
|
||||
@@ -257,7 +257,7 @@ func _on_exchange_completed(
|
||||
func _on_refresh_completed(
|
||||
_result: int, status: int,
|
||||
_headers: PackedStringArray, body: PackedByteArray) -> void:
|
||||
var json := JSON.parse_string(body.get_string_from_utf8())
|
||||
var json :Variant= JSON.parse_string(body.get_string_from_utf8())
|
||||
if status != 200 or json == null or not (json is Dictionary):
|
||||
auth_error.emit("Token refresh failed (HTTP %d)." % status)
|
||||
return
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://bqnvbrk2p83g1
|
||||
Reference in New Issue
Block a user