Files
sor/Client/origin-client/autoloads/Auth.gd
T
3aaabe75e3 Godot client networking + auth + login/main screens; fix admin hamburger
Server:
- ConnectionLoop: accept auth.login and auth.register as valid first messages
  (not just auth.hello); all ack payloads now include sessionToken for client
  persistence
- IGatewayAuthValidator: add LoginAsync and RegisterAsync
- GatewayAuthValidator: implement both via auth gRPC; fetch displayName from
  ValidateAsync after login/register succeeds
- Admin hamburger: custom .sor-admin-hamburger CSS (hardcoded white, no Bootstrap
  btn-link theme fighting); topbar z-index 1050 with position:sticky

Client (Godot 4.5, GDScript):
- autoloads/Network.gd: WebSocketPeer state machine (CONNECTING / CONNECTED /
  RECONNECTING), exponential backoff (1→30s), app-level keepalive ping every
  30s of idle, 30s ping timeout → reconnect, request/reply correlation by
  sequence number, debug_log_packets export flag for full packet dumps
- autoloads/Auth.gd: drives auth.hello / auth.login / auth.register as first
  WS message; persists sessionToken + userId + displayName to user://auth.cfg;
  auto-resumes saved session on reconnect; auth_success / auth_failed signals
- scenes/login/LoginScreen.tscn + login_screen.gd: Login + Register tabs,
  touch-friendly inputs (44px height), status label, Enter key submits, handles
  reconnect/resume states
- scenes/main/MainScreen.tscn + main_screen.gd: top bar with live connection
  dot, welcome label, placeholder nav buttons, sign-out, redirects to login on
  session expiry
- project.godot: Network and Auth autoloads registered; LoginScreen as main scene

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>
2026-02-27 20:48:53 -05:00

108 lines
3.8 KiB
GDScript

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)