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>
130 lines
5.2 KiB
GDScript
130 lines
5.2 KiB
GDScript
extends Control
|
|
## Login / Register screen.
|
|
##
|
|
## Shown as the entry point before authentication. Uses two tabs:
|
|
## "Login" — email + password → auth.login
|
|
## "Register" — email + password + display name → auth.register
|
|
##
|
|
## Listens to Auth.auth_success and Auth.auth_failed.
|
|
## On success: transitions to InitScreen (which runs version check + content sync,
|
|
## then proceeds to MainScreen).
|
|
## On failure: shows an error message and re-enables the form.
|
|
|
|
# ── Node references (unique names set in the .tscn) ──────────────────────────
|
|
|
|
@onready var email_input : LineEdit = %EmailInput
|
|
@onready var password_input : LineEdit = %PasswordInput
|
|
@onready var login_btn : Button = %LoginBtn
|
|
|
|
@onready var reg_email_input : LineEdit = %RegEmailInput
|
|
@onready var reg_password_input : LineEdit = %RegPasswordInput
|
|
@onready var reg_name_input : LineEdit = %RegNameInput
|
|
@onready var register_btn : Button = %RegisterBtn
|
|
|
|
@onready var status_label : Label = %StatusLabel
|
|
|
|
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
|
|
|
func _ready() -> void:
|
|
# If already authenticated (e.g. scene reloaded), skip straight to main.
|
|
if not Auth.user_id.is_empty():
|
|
_go_to_main()
|
|
return
|
|
|
|
Auth.auth_success.connect(_on_auth_success)
|
|
Auth.auth_failed.connect(_on_auth_failed)
|
|
|
|
login_btn.pressed.connect(_on_login_pressed)
|
|
register_btn.pressed.connect(_on_register_pressed)
|
|
|
|
# Allow pressing Enter to submit the login form.
|
|
password_input.text_submitted.connect(func(_t): _on_login_pressed())
|
|
reg_name_input.text_submitted.connect(func(_t): _on_register_pressed())
|
|
|
|
# Show connection / session-resume status on first open.
|
|
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)
|
|
|
|
# ── Button handlers ───────────────────────────────────────────────────────────
|
|
|
|
func _on_login_pressed() -> void:
|
|
var email := email_input.text.strip_edges()
|
|
var pass_ := password_input.text
|
|
if email.is_empty() or pass_.is_empty():
|
|
_set_status("Please enter your email and password.", true)
|
|
return
|
|
_set_status("Signing in…", false)
|
|
_set_enabled(false)
|
|
Auth.login(email, pass_)
|
|
|
|
|
|
func _on_register_pressed() -> void:
|
|
var email := reg_email_input.text.strip_edges()
|
|
var pass_ := reg_password_input.text
|
|
var name_ := reg_name_input.text.strip_edges()
|
|
if email.is_empty() or pass_.is_empty() or name_.is_empty():
|
|
_set_status("Please fill in all fields.", true)
|
|
return
|
|
_set_status("Creating account…", false)
|
|
_set_enabled(false)
|
|
Auth.register(email, pass_, name_)
|
|
|
|
# ── Auth signal handlers ──────────────────────────────────────────────────────
|
|
|
|
func _on_auth_success(_user_id: String, _display_name: String) -> void:
|
|
_go_to_main()
|
|
|
|
|
|
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:
|
|
login_btn.disabled = not enabled
|
|
register_btn.disabled = not enabled
|
|
|
|
|
|
func _friendly_error(code: String, message: String) -> String:
|
|
match code:
|
|
"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."
|
|
"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
|