extends Node ## Authentication autoload. ## ## Drives the auth handshake (auth.hello / auth.login / auth.register) as the ## first WebSocket message on every connection. Persists the session token to ## disk so the next session can reconnect silently. ## ## Scenes interact with this node via signals and the three public methods. ## ## Signals: ## auth_success(user_id, display_name) — auth succeeded; switch to main screen ## auth_failed(error_code, message) — auth rejected; show error to user const SAVE_PATH := "user://auth.cfg" const SECTION := "auth" signal auth_success(user_id: String, display_name: String) signal auth_failed(error_code: String, message: String) ## Set after a successful auth. Empty while unauthenticated. var user_id: String = "" var display_name: String = "" var session_token: String = "" # ── Lifecycle ───────────────────────────────────────────────────────────────── func _ready() -> void: _load_session() Network.connected.connect(_on_network_connected) # ── Public API ──────────────────────────────────────────────────────────────── ## Attempt to sign in with email and password. ## Call from the login form. func login(email: String, password: String) -> void: Network.request( "auth.login", { "email": email, "password": password }, func(_type: String, p: Variant): _on_auth_ok(p), func(code: String, msg: String): auth_failed.emit(code, msg) ) ## Attempt to create a new account. ## Call from the register form. func register(email: String, password: String, disp_name: String) -> void: Network.request( "auth.register", { "email": email, "password": password, "displayName": disp_name }, func(_type: String, p: Variant): _on_auth_ok(p), func(code: String, msg: String): auth_failed.emit(code, msg) ) ## Clear the saved session and reset auth state (e.g. logout button). func logout() -> void: user_id = "" display_name = "" session_token = "" _save_session() # ── Private ─────────────────────────────────────────────────────────────────── ## Called every time the WebSocket connects (including after reconnects). ## If a session token is saved, send auth.hello to resume silently. func _on_network_connected() -> void: if session_token.is_empty(): return # No token — LoginScreen will drive login/register. Network.request( "auth.hello", { "sessionToken": session_token }, func(_type: String, p: Variant): _on_auth_ok(p), func(code: String, msg: String): # Keep the token on network errors — we'll retry on the next # reconnect. Only clear it on explicit auth rejections. if code != "DISCONNECTED": session_token = "" _save_session() auth_failed.emit(code, msg) ) func _on_auth_ok(payload: Variant) -> void: var p := payload as Dictionary user_id = p.get("userId", "") display_name = p.get("displayName", "") session_token = p.get("sessionToken", "") _save_session() auth_success.emit(user_id, display_name) func _load_session() -> void: var cfg := ConfigFile.new() if cfg.load(SAVE_PATH) != OK: return session_token = cfg.get_value(SECTION, "session_token", "") user_id = cfg.get_value(SECTION, "user_id", "") display_name = cfg.get_value(SECTION, "display_name", "") func _save_session() -> void: var cfg := ConfigFile.new() cfg.set_value(SECTION, "session_token", session_token) cfg.set_value(SECTION, "user_id", user_id) cfg.set_value(SECTION, "display_name", display_name) cfg.save(SAVE_PATH)