Added auth

This commit is contained in:
Connor Johnstone
2026-03-19 14:02:33 -04:00
parent 93392db27c
commit 421ec3199b
21 changed files with 719 additions and 26 deletions

View File

@@ -55,6 +55,49 @@ async fn delete(url: &str) -> Result<(), ApiError> {
Ok(())
}
// --- Auth ---
pub async fn check_setup_required() -> Result<SetupRequired, ApiError> {
get_json(&format!("{BASE}/auth/setup-required")).await
}
pub async fn setup(username: &str, password: &str) -> Result<UserInfo, ApiError> {
let body = serde_json::json!({"username": username, "password": password}).to_string();
post_json(&format!("{BASE}/auth/setup"), &body).await
}
pub async fn login(username: &str, password: &str) -> Result<UserInfo, ApiError> {
let body = serde_json::json!({"username": username, "password": password}).to_string();
post_json(&format!("{BASE}/auth/login"), &body).await
}
pub async fn logout() -> Result<(), ApiError> {
let resp = Request::post(&format!("{BASE}/auth/logout"))
.send()
.await
.map_err(|e| ApiError(e.to_string()))?;
if !resp.ok() {
return Err(ApiError(format!("HTTP {}", resp.status())));
}
Ok(())
}
pub async fn get_me() -> Result<UserInfo, ApiError> {
get_json(&format!("{BASE}/auth/me")).await
}
pub async fn list_users() -> Result<Vec<UserInfo>, ApiError> {
get_json(&format!("{BASE}/auth/users")).await
}
pub async fn create_user(username: &str, password: &str) -> Result<UserInfo, ApiError> {
let body = serde_json::json!({"username": username, "password": password}).to_string();
post_json(&format!("{BASE}/auth/users"), &body).await
}
pub async fn delete_user(id: i32) -> Result<(), ApiError> {
delete(&format!("{BASE}/auth/users/{id}")).await
}
// --- Status ---
pub async fn get_status() -> Result<Status, ApiError> {
get_json(&format!("{BASE}/status")).await