client: fix auth-expiry reconnect loop + improve error messages

Network.gd — drain packets before handling close (main bug fix):
  When the server rejects auth.hello it sends an error frame then closes
  the connection. Godot's WebSocketPeer.poll() can transition directly to
  STATE_CLOSED while the error packet sits unread in the buffer. The old
  code returned early on STATE_CLOSED without calling _poll_packets(), so
  _on_ws_closed() fired DISCONNECTED for the pending auth.hello instead of
  the real AUTH_FAILED. Auth kept the session token and retried forever.
  Fix: drain any buffered packets before _on_ws_closed().

Network.gd — fail request() immediately when not connected:
  Previously request() silently dropped the send but left a dangling
  pending entry that was never resolved until the next disconnect. Now
  on_error("DISCONNECTED") is called immediately, giving the caller
  consistent feedback and keeping _pending clean.

main_screen.gd — stay on screen for DISCONNECTED auth errors:
  A transient network drop during auth.hello was sending the user back
  to the login screen unnecessarily. Only navigate to login when the
  session is explicitly rejected (AUTH_FAILED, ACCOUNT_LOCKED, etc.).
  The "● Reconnecting…" status label already handles the transient case.

login_screen.gd — add missing friendly error translations:
  INVALID_PASSWORD / USER_NOT_FOUND → "Wrong email or password."
  DISPLAY_NAME_TAKEN, ACCOUNT_LOCKED, AUTH_REQUIRED,
  AUTH_SERVICE_UNAVAILABLE all now map to clear human-readable strings
  instead of falling through to the raw server message or error code.

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:
dingo
2026-03-01 10:17:03 -05:00
co-authored by Claude Happy
parent 0607f02e93
commit 4322732b47
3 changed files with 28 additions and 4 deletions
+14
View File
@@ -81,6 +81,14 @@ func _process(delta: float) -> void:
WebSocketPeer.STATE_CLOSED:
if _state == _State.CONNECTED:
# Drain any packets the server sent just before closing (e.g. an
# auth error response followed immediately by the close frame).
# Without this, both arrive in the same poll() and we return early
# here, so _poll_packets() is skipped and _on_ws_closed() fires
# DISCONNECTED for all pending requests before the real error is
# dispatched — causing auth.hello failures to look like reconnect
# drops and keeping the session token alive indefinitely.
_poll_packets()
_on_ws_closed()
else: # STATE_CONNECTING → connection attempt failed
_schedule_reconnect()
@@ -107,6 +115,12 @@ func request(
on_error: Callable = Callable()) -> int:
_seq += 1
var seq := _seq
if _state != _State.CONNECTED:
# Fail immediately rather than dropping the send silently and leaving a
# dangling pending entry that never gets a response.
if on_error.is_valid():
on_error.call("DISCONNECTED", "Not connected.")
return seq
_pending[seq] = { &"on_reply": on_reply, &"on_error": on_error }
_send_raw({ "type": type, "seq": seq, "payload": payload })
return seq
@@ -113,11 +113,17 @@ func _set_enabled(enabled: bool) -> void:
func _friendly_error(code: String, message: String) -> String:
match code:
"INVALID_CREDENTIALS": return "Wrong email or password."
"INVALID_CREDENTIALS", \
"INVALID_PASSWORD", \
"USER_NOT_FOUND": return "Wrong email or password."
"EMAIL_TAKEN": return "That email is already in use."
"DISPLAY_NAME_TAKEN": return "That display name is already in use."
"WEAK_PASSWORD": return "Password must be at least 8 characters."
"INVALID_EMAIL": return "Please enter a valid email address."
"AUTH_FAILED": return "Authentication failed. Please try again."
"ACCOUNT_LOCKED": return "Account locked. Please try again later."
"AUTH_FAILED": return "Session expired. Please sign in again."
"AUTH_REQUIRED": return "Please sign in to continue."
"AUTH_SERVICE_UNAVAILABLE": return "Server unavailable. Please try again shortly."
_:
return message if not message.is_empty() \
else "Something went wrong (%s)." % code
@@ -47,8 +47,12 @@ func _update_status() -> void:
status_label.modulate = Color(1.0, 0.8, 0.2)
func _on_auth_failed(_error_code: String, _message: String) -> void:
# Session expired or invalidated — return to login.
func _on_auth_failed(error_code: String, _message: String) -> void:
if error_code == "DISCONNECTED":
# Transient drop during auth.hello retry — stay on screen.
# _update_status() already shows "● Reconnecting…" via the disconnected signal.
return
# Session explicitly rejected (expired, revoked, locked out) — go to login.
get_tree().change_scene_to_file("res://scenes/login/LoginScreen.tscn")