diff --git a/Client/origin-client/autoloads/Auth.gd b/Client/origin-client/autoloads/Auth.gd index 3d3908a..130587c 100644 --- a/Client/origin-client/autoloads/Auth.gd +++ b/Client/origin-client/autoloads/Auth.gd @@ -10,23 +10,46 @@ extends Node ## Signals: ## auth_success(user_id, display_name) — auth succeeded; switch to main screen ## auth_failed(error_code, message) — auth rejected; show error to user +## session_expired — auto-logout fired after disconnect timeout const SAVE_PATH := "user://auth.cfg" const SECTION := "auth" +## Saved sessions older than this are discarded on load (forces re-login). +const SESSION_TTL_DAYS := 30 + +## If the WebSocket stays disconnected for this many minutes while logged in, +## automatically clear the session and return to the login screen. +const DISCONNECT_LOGOUT_MIN := 30 + signal auth_success(user_id: String, display_name: String) signal auth_failed(error_code: String, message: String) +signal session_expired ## Fired when the disconnect timeout logs the user out. ## Set after a successful auth. Empty while unauthenticated. var user_id: String = "" var display_name: String = "" var session_token: String = "" +## Monotonic timestamp (seconds) when the last disconnect started; -1 = connected. +var _disconnect_at: float = -1.0 + # ── Lifecycle ───────────────────────────────────────────────────────────────── func _ready() -> void: _load_session() Network.connected.connect(_on_network_connected) + Network.disconnected.connect(_on_network_disconnected) + + +func _process(_delta: float) -> void: + if _disconnect_at < 0.0 or session_token.is_empty(): + return + if _now() - _disconnect_at >= DISCONNECT_LOGOUT_MIN * 60.0: + _disconnect_at = -1.0 + logout() + session_expired.emit() + get_tree().change_scene_to_file("res://scenes/login/LoginScreen.tscn") # ── Public API ──────────────────────────────────────────────────────────────── @@ -52,7 +75,7 @@ func register(email: String, password: String, disp_name: String) -> void: ) -## Clear the saved session and reset auth state (e.g. logout button). +## Clear the saved session and reset auth state. func logout() -> void: user_id = "" display_name = "" @@ -62,8 +85,9 @@ func logout() -> void: # ── Private ─────────────────────────────────────────────────────────────────── ## Called every time the WebSocket connects (including after reconnects). -## If a session token is saved, send auth.hello to resume silently. +## Cancels the disconnect timer. If a session token is saved, sends auth.hello. func _on_network_connected() -> void: + _disconnect_at = -1.0 # Cancel any pending auto-logout. if session_token.is_empty(): return # No token — LoginScreen will drive login/register. @@ -81,6 +105,13 @@ func _on_network_connected() -> void: ) +## Called every time the WebSocket disconnects. +## Starts the auto-logout countdown (only if logged in). +func _on_network_disconnected() -> void: + if not session_token.is_empty(): + _disconnect_at = _now() + + func _on_auth_ok(payload: Variant) -> void: var p := payload as Dictionary user_id = p.get("userId", "") @@ -94,6 +125,12 @@ func _load_session() -> void: var cfg := ConfigFile.new() if cfg.load(SAVE_PATH) != OK: return + # Discard sessions older than SESSION_TTL_DAYS. + var saved_at: int = cfg.get_value(SECTION, "saved_at", 0) + if saved_at > 0: + var age_days := (int(Time.get_unix_time_from_system()) - saved_at) / 86400 + if age_days >= SESSION_TTL_DAYS: + return # Expired — treat as fresh launch. session_token = cfg.get_value(SECTION, "session_token", "") user_id = cfg.get_value(SECTION, "user_id", "") display_name = cfg.get_value(SECTION, "display_name", "") @@ -104,4 +141,11 @@ func _save_session() -> void: cfg.set_value(SECTION, "session_token", session_token) cfg.set_value(SECTION, "user_id", user_id) cfg.set_value(SECTION, "display_name", display_name) + # Only write a timestamp when there is an active session to expire. + if not session_token.is_empty(): + cfg.set_value(SECTION, "saved_at", int(Time.get_unix_time_from_system())) cfg.save(SAVE_PATH) + + +func _now() -> float: + return Time.get_ticks_msec() / 1000.0 diff --git a/Client/origin-client/autoloads/Network.gd b/Client/origin-client/autoloads/Network.gd index 932d057..8ea5d0b 100644 --- a/Client/origin-client/autoloads/Network.gd +++ b/Client/origin-client/autoloads/Network.gd @@ -16,7 +16,7 @@ extends Node const SERVER_URL := "wss://hub.aeternalisgames.com/ws" ## Print every sent/received packet as JSON to stdout. -@export var debug_log_packets: bool = false +@export var debug_log_packets: bool = true const KEEPALIVE_INTERVAL_SEC := 30.0 ## Idle seconds before sending an app-level ping. const KEEPALIVE_TIMEOUT_SEC := 30.0 ## Seconds with no reply before forcing a reconnect.