Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

Refactor hotmart_login to return email instead of token and implement hotmart_check_session for session validation

Tonhoe11b5fa3a3a9
diff --git a/src-tauri/src/commands/auth.rs b/src-tauri/src/commands/auth.rs
index 60687d16..6d2d7596 100644
--- a/src-tauri/src/commands/auth.rs
+++ b/src-tauri/src/commands/auth.rs
@@ -11,16 +11,11 @@ pub async fn hotmart_login(
match authenticate(&email, &password).await {
Ok(session) => {
- let token = session.token.clone();
+ let response_email = session.email.clone();
let mut guard = state.hotmart_session.lock().await;
*guard = Some(session);
tracing::info!("Sessão Hotmart salva no state global");
-
- Ok(serde_json::to_string(&serde_json::json!({
- "token": token,
- "success": true
- }))
- .unwrap())
+ Ok(response_email)
}
Err(e) => {
tracing::error!("Falha no login Hotmart: {}", e);
@@ -28,3 +23,46 @@ pub async fn hotmart_login(
}
}
}
+
+#[tauri::command]
+pub async fn hotmart_check_session(
+ state: tauri::State<'_, AppState>,
+) -> Result<String, String> {
+ let guard = state.hotmart_session.lock().await;
+ let session = guard
+ .as_ref()
+ .ok_or_else(|| "not_authenticated".to_string())?;
+
+ let token = session.token.clone();
+ let email = session.email.clone();
+ let client = session.client.clone();
+ drop(guard);
+
+ let resp = client
+ .get(format!(
+ "https://api-sec-vlc.hotmart.com/security/oauth/check_token?token={}",
+ token
+ ))
+ .header("Authorization", format!("Bearer {}", token))
+ .send()
+ .await
+ .map_err(|e| format!("Erro na validação: {}", e))?;
+
+ if resp.status().is_success() {
+ tracing::info!("Sessão Hotmart válida para {}", email);
+ Ok(email)
+ } else {
+ tracing::info!("Sessão Hotmart expirada, limpando state");
+ state.hotmart_session.lock().await.take();
+ Err("session_expired".to_string())
+ }
+}
+
+#[tauri::command]
+pub async fn hotmart_logout(
+ state: tauri::State<'_, AppState>,
+) -> Result<(), String> {
+ state.hotmart_session.lock().await.take();
+ tracing::info!("Sessão Hotmart removida");
+ Ok(())
+}