Working auth

This commit is contained in:
2026-04-04 19:09:12 +02:00
parent 31033d6a05
commit 03a0b360e0
2 changed files with 46 additions and 14 deletions
+39 -13
View File
@@ -1,7 +1,7 @@
use std::{fs, path::PathBuf};
use reqwest::Client;
use tauri::{command, Url};
use tauri::{command, AppHandle, Emitter, Manager, Url};
use lyceris::auth::microsoft::{authenticate, create_link, refresh, validate, MinecraftAccount};
@@ -34,21 +34,47 @@ fn save_accounts(accounts: &[MinecraftAccount]) -> Result<(), String> {
fs::write(path, data).map_err(|e| e.to_string())
}
fn extract_code(url: &str) -> Option<String> {
Url::parse(url)
.ok()?
.query_pairs()
.find(|(key, _)| key == "code")
.map(|(_, value)| value.to_string())
}
#[command]
pub fn auth_create_link(app: tauri::AppHandle) -> Result<String, String> {
pub fn auth_create_link(app: AppHandle) -> Result<String, String> {
let link = create_link().map_err(|e| e.to_string())?;
// Create auth window
tauri::WebviewWindowBuilder::new(
&app,
"auth",
tauri::WebviewUrl::External(link.parse::<Url>().map_err(|e| e.to_string())?),
)
.title("Microsoft Login")
.inner_size(500.0, 700.0)
.resizable(false)
.build()
.map_err(|e| e.to_string())?;
let url = link.parse::<Url>().map_err(|e| e.to_string())?;
let app_handle = app.clone();
tauri::WebviewWindowBuilder::new(&app, "auth", tauri::WebviewUrl::External(url))
.title("Microsoft Login")
.inner_size(500.0, 700.0)
.resizable(false)
.on_navigation(move |nav_url| {
let url_str = nav_url.to_string();
// Check for auth code
if url_str.contains("code=") {
if let Some(code) = extract_code(&url_str) {
println!("Auth code received: {}", code);
let _ = app_handle.emit("auth-code", code);
if let Some(win) = app_handle.get_webview_window("auth") {
let _ = win.close();
}
}
}
// Always allow navigation
true
})
.build()
.map_err(|e| e.to_string())?;
Ok(link)
}