From 0505cd7ff17711a95e1e1abb17309e981af10ed2 Mon Sep 17 00:00:00 2001 From: coder-bot Date: Thu, 20 Aug 2026 14:03:53 +0000 Subject: [PATCH] WIP checkpoint: Fix: navigating Play -> Back logs the user out (should stay logged in) --- scripts/main_menu.gd | 54 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/scripts/main_menu.gd b/scripts/main_menu.gd index 6cea46e..a38ae4e 100644 --- a/scripts/main_menu.gd +++ b/scripts/main_menu.gd @@ -8,9 +8,14 @@ extends Control ## ## On successful auth the token is stored via ApiClient, AuthButtonsRow hides, ## the welcome label shows the username, and the Play button becomes enabled. +## +## When returning from another scene (e.g. world_list), _ready() checks whether +## ApiClient already holds a valid token. If so, it restores the authenticated +## UI state (Play enabled, welcome label visible) instead of forcing the user +## back to the login form. -# ── UI references ──────────────────────────────────────────────────────────── +# ─── UI references ──────────────────────────────────────────────────────────── var _auth_buttons_row: HBoxContainer var _login_button: Button @@ -32,7 +37,7 @@ var _is_register_mode: bool = false var _is_authenticating: bool = false -# ── Lifecycle ──────────────────────────────────────────────────────────────── +# ─── Lifecycle ──────────────────────────────────────────────────────────────── func _ready() -> void: # Cache node references. @@ -52,7 +57,38 @@ func _ready() -> void: _play_button = $CenterContainer/VBoxContainer/PlayButton _quit_button = $CenterContainer/VBoxContainer/QuitButton - # Initial state. + # Check whether a valid session already exists (e.g. user logged in, + # navigated to world_list, then clicked Back). If so, restore the + # authenticated UI state instead of forcing the login form again. + if ApiClient.has_token(): + var me_result: Dictionary = await ApiClient.get_me() + if me_result.ok and me_result.data is Dictionary: + var username: String = str((me_result.data as Dictionary).get("username", "")) + if username != "": + _show_authenticated(username) + return + + # Token missing or invalid — fall through to logged-out state. + _reset_to_logged_out() + + +## Restore the UI to the authenticated state (Play enabled, welcome label, +## auth buttons hidden). +func _show_authenticated(username: String) -> void: + _auth_panel.visible = false + _status_label.visible = false + _welcome_label.text = "Welcome, %s!" % username + _welcome_label.visible = true + _email_edit.visible = false + _dob_edit.visible = false + _play_button.disabled = false + _auth_buttons_row.visible = false + print("Restored authenticated session for: ", username) + + +## Reset the UI to the logged-out state (Play disabled, auth panel hidden, +## login/register buttons visible). +func _reset_to_logged_out() -> void: _auth_panel.visible = false _status_label.visible = false _welcome_label.visible = false @@ -62,7 +98,7 @@ func _ready() -> void: _update_auth_mode_ui() -# ── Play / Quit ────────────────────────────────────────────────────────────── +# ─── Play / Quit ───────────────────────────────────────────────────────────── func _on_play_button_pressed() -> void: if _play_button.disabled: @@ -76,7 +112,7 @@ func _on_quit_button_pressed() -> void: get_tree().quit() -# ── Auth panel toggle ──────────────────────────────────────────────────────── +# ─── Auth panel toggle ─────────────────────────────────────────────────────── func _on_login_button_pressed() -> void: """Show the auth panel, called from AuthButtonsRow/LoginButton.""" @@ -94,7 +130,7 @@ func _on_register_button_pressed() -> void: _username_edit.grab_focus() -# ── Mode toggle (Login / Register) ─────────────────────────────────────────── +# ─── Mode toggle (Login / Register) ────────────────────────────────────────── func _on_mode_toggle_toggled(button_pressed: bool) -> void: _is_register_mode = button_pressed @@ -116,7 +152,7 @@ func _update_auth_mode_ui() -> void: _dob_edit.visible = false -# ── Auth submission ────────────────────────────────────────────────────────── +# ─── Auth submission ───────────────────────────────────────────────────────── func _on_auth_button_pressed() -> void: """Handle login or register button press.""" @@ -187,7 +223,7 @@ func _on_auth_complete(result: Dictionary) -> void: _show_status(result.detail) -# ── Status helpers ─────────────────────────────────────────────────────────── +# ─── Status helpers ────────────────────────────────────────────────────────── func _show_status(message: String) -> void: _status_label.text = message @@ -199,7 +235,7 @@ func _clear_status() -> void: _status_label.visible = false -# ── Validation ─────────────────────────────────────────────────────────────── +# ─── Validation ────────────────────────────────────────────────────────────── func _is_valid_date(date_str: String) -> bool: """Check if a string looks like a valid YYYY-MM-DD date."""