1
0
Files
AstralRinth/packages/app-lib/src/api/minecraft_auth.rs
aecsocket 93b79759c7 Add auth servers unreachable warning to app (#4774)
* Add auth servers unreachable warning to app

* Check auth status every 5 minutes

* Use admonition in auth server warning

* feat: tanstack

* Fix auth server reachability query

* Format

* intl extract

---------

Co-authored-by: Calum H. (IMB11) <contact@cal.engineer>
2025-11-17 18:41:52 +00:00

91 lines
2.4 KiB
Rust

//! Authentication flow interface
use reqwest::StatusCode;
use crate::State;
use crate::state::{Credentials, MinecraftLoginFlow};
use crate::util::fetch::REQWEST_CLIENT;
#[tracing::instrument]
pub async fn check_reachable() -> crate::Result<()> {
let resp = REQWEST_CLIENT
.get("https://api.minecraftservices.com/entitlements/mcstore")
.send()
.await?;
if resp.status() == StatusCode::UNAUTHORIZED {
return Ok(());
}
resp.error_for_status()?;
Ok(())
}
#[tracing::instrument]
pub async fn begin_login() -> crate::Result<MinecraftLoginFlow> {
let state = State::get().await?;
crate::state::login_begin(&state.pool).await
}
#[tracing::instrument]
pub async fn finish_login(
code: &str,
flow: MinecraftLoginFlow,
) -> crate::Result<Credentials> {
let state = State::get().await?;
crate::state::login_finish(code, flow, &state.pool).await
}
#[tracing::instrument]
pub async fn get_default_user() -> crate::Result<Option<uuid::Uuid>> {
let state = State::get().await?;
let user = Credentials::get_active(&state.pool).await?;
Ok(user.map(|user| user.offline_profile.id))
}
#[tracing::instrument]
pub async fn set_default_user(user: uuid::Uuid) -> crate::Result<()> {
let state = State::get().await?;
let users = Credentials::get_all(&state.pool).await?;
let (_, mut user) = users.remove(&user).ok_or_else(|| {
crate::ErrorKind::OtherError(format!(
"Tried to get nonexistent user with ID {user}"
))
.as_error()
})?;
user.active = true;
user.upsert(&state.pool).await?;
Ok(())
}
/// Remove a user account from the database
#[tracing::instrument]
pub async fn remove_user(uuid: uuid::Uuid) -> crate::Result<()> {
let state = State::get().await?;
let users = Credentials::get_all(&state.pool).await?;
if let Some((uuid, user)) = users.remove(&uuid) {
Credentials::remove(uuid, &state.pool).await?;
if user.active
&& let Some((_, mut user)) = users.into_iter().next()
{
user.active = true;
user.upsert(&state.pool).await?;
}
}
Ok(())
}
/// Get a copy of the list of all user credentials
#[tracing::instrument]
pub async fn users() -> crate::Result<Vec<Credentials>> {
let state = State::get().await?;
let users = Credentials::get_all(&state.pool).await?;
Ok(users.into_iter().map(|x| x.1).collect())
}