fix(network): isolate auth sessions for multiple instances on same pc

This commit is contained in:
2026-07-13 11:40:27 +08:00
parent 8718e2d245
commit dea822987b
3 changed files with 42 additions and 34 deletions
-1
View File
@@ -18,4 +18,3 @@ The generated code is designed to be supported Godot Engine `3.1+`.
### Limitations ### Limitations
The code generator has __only__ been checked against the Swagger specification generated for Nakama server. YMMV. The code generator has __only__ been checked against the Swagger specification generated for Nakama server. YMMV.
+11 -2
View File
@@ -21,13 +21,19 @@ var is_guest: bool = false
var auth_mode: AuthMode = AuthMode.GUEST var auth_mode: AuthMode = AuthMode.GUEST
# Session persistence # Session persistence
const SESSION_FILE := "user://auth_session.dat" var SESSION_FILE := "user://auth_session.dat"
const CREDENTIALS_FILE := "user://auth_credentials.dat" var CREDENTIALS_FILE := "user://auth_credentials.dat"
# Encryption key for session storage (device-specific) # Encryption key for session storage (device-specific)
var ENCRYPTION_KEY: String = OS.get_unique_id().sha256_text() var ENCRYPTION_KEY: String = OS.get_unique_id().sha256_text()
func _ready() -> void: func _ready() -> void:
# In debug mode, suffix session files with the process ID to allow multiple instances
if OS.is_debug_build():
var suffix = "_" + str(OS.get_process_id())
SESSION_FILE = "user://auth_session%s.dat" % suffix
CREDENTIALS_FILE = "user://auth_credentials%s.dat" % suffix
# Try to restore session on startup # Try to restore session on startup
call_deferred("_try_restore_session") call_deferred("_try_restore_session")
@@ -155,6 +161,9 @@ func login_as_guest() -> bool:
func _get_device_id() -> String: func _get_device_id() -> String:
# Try to load saved device ID for consistent guest identity # Try to load saved device ID for consistent guest identity
var id_file := "user://device_id.txt" var id_file := "user://device_id.txt"
if OS.is_debug_build():
id_file = "user://device_id_%s.txt" % str(OS.get_process_id())
if FileAccess.file_exists(id_file): if FileAccess.file_exists(id_file):
var file_read := FileAccess.open(id_file, FileAccess.READ) var file_read := FileAccess.open(id_file, FileAccess.READ)
if file_read: if file_read: