PC native — connection lost, retrying:
_send_keycloak_auth's DISCONNECTED handler was clearing _pending_keycloak_token
(taken at top of function) without restoring it, so the next _on_network_connected
had no token to retry with. Restore it: _pending_keycloak_token = token.
Web export — could not bind a local redirect port:
Godot's HTML5 export cannot create TCP server sockets. Add a separate web
code path in Keycloak.gd:
- start_auth_flow() detects OS.get_name() == "Web" and navigates the page
to Keycloak, persisting PKCE state (verifier, state, redirect_uri) in
sessionStorage before the page leaves.
- _check_web_callback() (called in _ready()) detects the ?code= params on
return, recovers state from sessionStorage, cleans the URL via
history.replaceState, then calls _exchange_code().
- auth_in_progress flag lets login_screen show "Completing sign-in..."
while the token exchange is in flight after page reload.
Requires the web game's origin to be registered as a valid redirect URI
in the Keycloak sor-game client.
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>
125 lines
5.1 KiB
GDScript
125 lines
5.1 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.
|
|
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
|