Add session expiry renewal flow

This commit is contained in:
hz
2026-07-18 15:53:11 -05:00
parent 8c349314ad
commit a232e18c46
14 changed files with 575 additions and 21 deletions
+25 -10
View File
@@ -45,6 +45,10 @@ var pending_report_question_id := ""
var attempt_queue: Array = []
var active_attempt_index := -1
var attempt_request_active := false
var questions_request_token := ""
var attempt_request_token := ""
var rating_request_token := ""
var report_request_token := ""
var pending_excluded_question_ids: Array = []
var pending_require_media := false
var image_question_search_attempts := 0
@@ -102,7 +106,7 @@ func _ready() -> void:
func load_questions_from_api(reset_retry: bool = true, excluded_question_ids: Array = [], require_media: bool = false) -> void:
if AuthState.access_token.is_empty():
if not AuthState.can_send_authenticated_request():
_set_load_error("Login required before loading questions.", false)
return
@@ -132,9 +136,10 @@ func load_questions_from_api(reset_retry: bool = true, excluded_question_ids: Ar
if should_load_ratings:
load_ratings_from_api()
questions_request_token = AuthState.access_token
var headers := PackedStringArray([
"Content-Type: application/json",
"Authorization: Bearer %s" % AuthState.access_token
"Authorization: Bearer %s" % questions_request_token
])
var payload := {
"limit": 1,
@@ -149,6 +154,8 @@ func load_questions_from_api(reset_retry: bool = true, excluded_question_ids: Ar
func _on_api_questions_loaded(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
is_loading = false
if response_code == 401:
AuthState.handle_unauthorized(questions_request_token)
if result != HTTPRequest.RESULT_SUCCESS or response_code < 200 or response_code >= 300:
var message := "The connection to the question service was interrupted."
if response_code > 0:
@@ -251,28 +258,29 @@ func clear_questions() -> void:
func record_first_attempt(question_id: String, choice_id: String, presented_choice_ids: Array = []) -> void:
if AuthState.access_token.is_empty() or question_id == "" or choice_id == "":
if not AuthState.can_send_authenticated_request() or question_id == "" or choice_id == "":
return
_enqueue_attempt("answer", question_id, choice_id, presented_choice_ids)
func skip_question(question_id: String) -> void:
if AuthState.access_token.is_empty() or question_id == "":
if not AuthState.can_send_authenticated_request() or question_id == "":
return
_enqueue_attempt("skip", question_id, "")
func submit_question_report(question_id: String, category: String, details: String) -> bool:
if AuthState.access_token.is_empty() or question_id == "":
if not AuthState.can_send_authenticated_request() or question_id == "":
question_report_failed.emit(question_id, "Login is required to submit a report.")
return false
if report_request.get_http_client_status() != HTTPClient.STATUS_DISCONNECTED:
question_report_failed.emit(question_id, "Another report is still being submitted.")
return false
report_request_token = AuthState.access_token
var headers := PackedStringArray([
"Content-Type: application/json",
"Authorization: Bearer %s" % AuthState.access_token
"Authorization: Bearer %s" % report_request_token
])
var payload := {
"questionExternalId": question_id,
@@ -294,10 +302,11 @@ func submit_question_report(question_id: String, category: String, details: Stri
func load_ratings_from_api() -> void:
if AuthState.access_token.is_empty():
if not AuthState.can_send_authenticated_request():
return
rating_request_token = AuthState.access_token
var headers := PackedStringArray([
"Authorization: Bearer %s" % AuthState.access_token
"Authorization: Bearer %s" % rating_request_token
])
var err := rating_request.request(ratings_api_url, headers, HTTPClient.METHOD_GET)
if err != OK:
@@ -397,7 +406,7 @@ func _enqueue_attempt(kind: String, question_id: String, choice_id: String, pres
func _process_attempt_queue() -> void:
if attempt_request_active or attempt_queue.is_empty() or AuthState.access_token.is_empty():
if attempt_request_active or attempt_queue.is_empty() or not AuthState.can_send_authenticated_request():
return
if attempt_retry_timer != null and not attempt_retry_timer.is_stopped():
return
@@ -411,9 +420,10 @@ func _process_attempt_queue() -> void:
return
var queued: Dictionary = attempt_queue[active_attempt_index]
attempt_request_token = AuthState.access_token
var headers := PackedStringArray([
"Content-Type: application/json",
"Authorization: Bearer %s" % AuthState.access_token
"Authorization: Bearer %s" % attempt_request_token
])
var question_id := str(queued.get("question_id", ""))
var url := question_attempt_api_url
@@ -460,6 +470,7 @@ func _on_first_attempt_recorded(result: int, response_code: int, _headers: Packe
var succeeded := result == HTTPRequest.RESULT_SUCCESS and response_code >= 200 and response_code < 300
if not succeeded:
if response_code == 401:
AuthState.handle_unauthorized(attempt_request_token)
active_attempt_index = -1
elif response_code in [400, 403, 404]:
push_warning("Discarding invalid question attempt with status %d." % response_code)
@@ -563,6 +574,8 @@ func _on_question_report_submitted(result: int, response_code: int, _headers: Pa
if question_id == "":
question_id = pending_report_question_id
pending_report_question_id = ""
if response_code == 401:
AuthState.handle_unauthorized(report_request_token)
if result != HTTPRequest.RESULT_SUCCESS or response_code < 200 or response_code >= 300:
var message := "The report could not be submitted. Please try again."
@@ -585,6 +598,8 @@ func _on_question_report_submitted(result: int, response_code: int, _headers: Pa
func _on_ratings_loaded(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
if response_code == 401:
AuthState.handle_unauthorized(rating_request_token)
if result != HTTPRequest.RESULT_SUCCESS or response_code < 200 or response_code >= 300:
push_warning("Ratings API request failed with status %d." % response_code)
return