PC — scene reloading on auth error: Auth.gd's keycloak error handler called change_scene_to_file(LoginScreen) after emitting auth_failed, which reloaded the scene and wiped the error message before the user could see it. Removed the scene change — the login screen is already active and handles auth_failed itself. Web — token_received signal never handled after page redirect: login_screen._ready() returned early when Keycloak.auth_in_progress was true, before connecting Keycloak.token_received. When the exchange completed nobody was listening, so the game sat frozen. Signals are now connected before the early-return check. Web — fragile redirect URI and sessionStorage injection: JavaScriptBridge.eval return value could stringify as "<null>". Now uses separate origin + pathname evals with a null check. sessionStorage items and the final navigation use uri_encode() + decodeURIComponent() instead of manual quote-escaping, preventing injection from PKCE values. 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>
126 lines
5.2 KiB
GDScript
126 lines
5.2 KiB
GDScript
extends Control
|
|
## Login screen — Keycloak Authorization Code + PKCE flow.
|
|
##
|
|
## Single "Sign in with Aeternalis" button triggers the browser-based OAuth2 flow.
|
|
## On success: Keycloak.token_received → Auth.login_with_keycloak → auth_success → InitScreen.
|
|
## On failure: Keycloak.auth_error or Auth.auth_failed → show error, re-enable button.
|
|
##
|
|
## Silent resume: if Auth already has a session token when this scene loads
|
|
## (e.g. app was restarted with a valid saved token), it skips straight to main.
|
|
|
|
# ── Node references ───────────────────────────────────────────────────────────
|
|
|
|
@onready var sign_in_btn : Button = %SignInBtn
|
|
@onready var status_label : Label = %StatusLabel
|
|
|
|
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
|
|
|
func _ready() -> void:
|
|
ContentCache.apply_scene_background(self)
|
|
|
|
# Already authenticated — skip straight to main (e.g. valid saved token).
|
|
if not Auth.user_id.is_empty():
|
|
_go_to_main.call_deferred()
|
|
return
|
|
|
|
Auth.auth_success.connect(_on_auth_success)
|
|
Auth.auth_failed.connect(_on_auth_failed)
|
|
Network.connected.connect(_on_network_connected)
|
|
|
|
Keycloak.token_received.connect(_on_token_received)
|
|
Keycloak.auth_error.connect(_on_keycloak_error)
|
|
|
|
sign_in_btn.pressed.connect(_on_sign_in_pressed)
|
|
|
|
# Web export: returning from Keycloak redirect — exchange is already in flight.
|
|
# Signals are connected above before this check so token_received is handled.
|
|
if Keycloak.auth_in_progress:
|
|
_set_status("Completing sign-in…", false)
|
|
_set_enabled(false)
|
|
return
|
|
|
|
# Show connecting / resume status while the socket is still coming up.
|
|
if not Auth.session_token.is_empty():
|
|
_set_status("Resuming session…", false)
|
|
_set_enabled(false)
|
|
elif not Network.is_online():
|
|
_set_status("Connecting…", false)
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
if Auth.auth_success.is_connected(_on_auth_success):
|
|
Auth.auth_success.disconnect(_on_auth_success)
|
|
if Auth.auth_failed.is_connected(_on_auth_failed):
|
|
Auth.auth_failed.disconnect(_on_auth_failed)
|
|
if Network.connected.is_connected(_on_network_connected):
|
|
Network.connected.disconnect(_on_network_connected)
|
|
if Keycloak.token_received.is_connected(_on_token_received):
|
|
Keycloak.token_received.disconnect(_on_token_received)
|
|
if Keycloak.auth_error.is_connected(_on_keycloak_error):
|
|
Keycloak.auth_error.disconnect(_on_keycloak_error)
|
|
|
|
# ── Button handler ────────────────────────────────────────────────────────────
|
|
|
|
func _on_sign_in_pressed() -> void:
|
|
_set_status("Opening browser…", false)
|
|
_set_enabled(false)
|
|
Keycloak.start_auth_flow()
|
|
|
|
# ── Keycloak signal handlers ──────────────────────────────────────────────────
|
|
|
|
func _on_token_received(access_token: String, refresh_token: String) -> void:
|
|
_set_status("Signing in…", false)
|
|
Auth.login_with_keycloak(access_token, refresh_token)
|
|
|
|
|
|
func _on_keycloak_error(message: String) -> void:
|
|
_set_status(message, true)
|
|
_set_enabled(true)
|
|
|
|
# ── Auth signal handlers ──────────────────────────────────────────────────────
|
|
|
|
func _on_auth_success(_user_id: String, _display_name: String) -> void:
|
|
_go_to_main()
|
|
|
|
|
|
func _on_network_connected() -> void:
|
|
# Socket is up but no session in flight — clear the spinner.
|
|
if Auth.session_token.is_empty() and status_label.text == "Connecting…":
|
|
_set_status("", false)
|
|
|
|
|
|
func _on_auth_failed(error_code: String, message: String) -> void:
|
|
if error_code == "DISCONNECTED":
|
|
_set_status("Connection lost. Retrying…", false)
|
|
else:
|
|
_set_status(_friendly_error(error_code, message), true)
|
|
_set_enabled(true)
|
|
|
|
# ── Helpers ───────────────────────────────────────────────────────────────────
|
|
|
|
func _go_to_main() -> void:
|
|
if Auth.needs_starter_deck:
|
|
get_tree().change_scene_to_file("res://scenes/starter_deck/StarterDeckScreen.tscn")
|
|
else:
|
|
get_tree().change_scene_to_file("res://scenes/init/InitScreen.tscn")
|
|
|
|
|
|
func _set_status(text: String, is_error: bool) -> void:
|
|
status_label.text = text
|
|
status_label.modulate = Color(1.0, 0.4, 0.4) if is_error else Color(0.8, 0.8, 0.8)
|
|
|
|
|
|
func _set_enabled(enabled: bool) -> void:
|
|
sign_in_btn.disabled = not enabled
|
|
|
|
|
|
func _friendly_error(code: String, message: String) -> String:
|
|
match code:
|
|
"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."
|
|
"MISSING_ROLE": return "Your account does not have access to this game."
|
|
_:
|
|
return message if not message.is_empty() \
|
|
else "Something went wrong (%s)." % code
|