feat: add login Tauri command

This commit is contained in:
2026-08-12 16:59:55 +02:00
parent 55133751cb
commit d384465f2e
3 changed files with 46 additions and 0 deletions

View File

@@ -0,0 +1 @@
pub mod user;

View File

@@ -0,0 +1,42 @@
use crate::models::{AuthTokenResponse, User};
use crate::state::{AppState, BASE_URL};
#[tauri::command]
pub async fn login(state: tauri::State<'_, AppState>, email: String) -> Result<User, String> {
let users: Vec<User> = state
.client
.get(format!("{}/users", BASE_URL))
.send()
.await
.map_err(|e| e.to_string())?
.json()
.await
.map_err(|e| e.to_string())?;
let user = users
.into_iter()
.find(|u| u.email == email)
.ok_or_else(|| "User not found".to_string())?;
let body = serde_json::json!({
"user_id": user.id,
"kind": "refresh_token",
"expires_at": "2027-01-01T00:00:00Z"
});
let token_resp: AuthTokenResponse = state
.client
.post(format!("{}/auth-tokens", BASE_URL))
.json(&body)
.send()
.await
.map_err(|e| e.to_string())?
.json()
.await
.map_err(|e| e.to_string())?;
*state.token.lock().unwrap() = Some(token_resp.token);
*state.user_id.lock().unwrap() = Some(user.id.clone());
Ok(user)
}

View File

@@ -1,11 +1,14 @@
mod commands;
mod models; mod models;
mod state; mod state;
use state::AppState; use state::AppState;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
tauri::Builder::default() tauri::Builder::default()
.manage(AppState::new()) .manage(AppState::new())
.invoke_handler(tauri::generate_handler![commands::user::login])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }