stepstead-client/autoload/auth.gd
jts 6a1706051d İlk commit: Stepstead Godot istemci (4.6 Mobile)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 11:31:35 +03:00

53 lines
1.5 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends Node
## Auth durumu — JWT token saklama ve auth API çağrıları.
const SAVE_PATH := "user://auth.dat"
var token: String = ""
var user: Dictionary = {}
func _ready() -> void:
_load()
func _save() -> void:
var f := FileAccess.open(SAVE_PATH, FileAccess.WRITE)
if f:
f.store_var({"token": token, "user": user})
func _load() -> void:
if not FileAccess.file_exists(SAVE_PATH):
return
var f := FileAccess.open(SAVE_PATH, FileAccess.READ)
if f:
var data: Variant = f.get_var()
if data is Dictionary:
token = data.get("token", "")
user = data.get("user", {})
func is_logged_in() -> bool:
return token != ""
func clear() -> void:
token = ""
user = {}
_save()
func register(email: String, password: String, display_name: String = "") -> Dictionary:
var body := {"email": email, "password": password}
if display_name != "":
body["display_name"] = display_name
return await Api.request("POST", "/auth/register", body, false)
func login(email: String, password: String) -> Dictionary:
var res: Dictionary = await Api.request("POST", "/auth/login", {"email": email, "password": password}, false)
if res.ok and res.body is Dictionary and res.body.has("access_token"):
token = res.body["access_token"]
_save()
var me_res: Dictionary = await Api.request("GET", "/auth/me", null, true)
if me_res.ok:
user = me_res.body
_save()
return res
func request_password_reset(email: String) -> Dictionary:
return await Api.request("POST", "/auth/request-password-reset", {"email": email}, false)