aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHsiangNianian <i@jyunko.cn>2026-01-18 12:16:55 +0800
committerHsiangNianian <i@jyunko.cn>2026-01-18 12:16:55 +0800
commit1021c921c5690ceb800c03140de0723f3338e121 (patch)
tree92d22f948049e40a4f9535231114926433adc75f
parent079ee0a6611499db68d1eb4894fab64739d5d2e7 (diff)
downloadDropOut-1021c921c5690ceb800c03140de0723f3338e121.tar.gz
DropOut-1021c921c5690ceb800c03140de0723f3338e121.zip
fix(auth): add token expiry check in start_game
Check if the Microsoft account token is expired before attempting to launch the game. If expired, attempt to refresh using the refresh_token. If refresh fails, return an error instructing the user to login again. Also removed #[allow(dead_code)] from is_token_expired since it's now actively used.
-rw-r--r--src-tauri/src/core/auth.rs1
-rw-r--r--src-tauri/src/main.rs31
2 files changed, 30 insertions, 2 deletions
diff --git a/src-tauri/src/core/auth.rs b/src-tauri/src/core/auth.rs
index e1e1588..d5e6c17 100644
--- a/src-tauri/src/core/auth.rs
+++ b/src-tauri/src/core/auth.rs
@@ -136,7 +136,6 @@ pub async fn refresh_microsoft_token(refresh_token: &str) -> Result<TokenRespons
}
/// Check if a Microsoft account token is expired or about to expire
-#[allow(dead_code)]
pub fn is_token_expired(expires_at: i64) -> bool {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 4086066..853c93e 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -82,13 +82,42 @@ async fn start_game(
// Check for active account
emit_log!(window, "Checking for active account...".to_string());
- let account = auth_state
+ let mut account = auth_state
.active_account
.lock()
.unwrap()
.clone()
.ok_or("No active account found. Please login first.")?;
+ // Check if Microsoft account token is expired and refresh if needed
+ if let core::auth::Account::Microsoft(ms_account) = &account {
+ if core::auth::is_token_expired(ms_account.expires_at) {
+ emit_log!(window, "Token expired, refreshing...".to_string());
+ match core::auth::refresh_full_auth(
+ &ms_account
+ .refresh_token
+ .clone()
+ .ok_or("No refresh token available")?,
+ )
+ .await
+ {
+ Ok((refreshed_account, _new_ms_refresh)) => {
+ let refreshed_account = core::auth::Account::Microsoft(refreshed_account);
+ *auth_state.active_account.lock().unwrap() = Some(refreshed_account.clone());
+ account = refreshed_account;
+ emit_log!(window, "Token refreshed successfully".to_string());
+ }
+ Err(e) => {
+ emit_log!(window, format!("Token refresh failed: {}", e));
+ return Err(format!(
+ "Your login session has expired. Please login again: {}",
+ e
+ ));
+ }
+ }
+ }
+ }
+
emit_log!(window, format!("Account found: {}", account.username()));
let config = config_state.config.lock().unwrap().clone();