Library improvements (#126)

* Base impl

* Add grouping

* Fix some styling things

* Run linter

* add missing features

* add dev mode

---------

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
This commit is contained in:
Adrian O.V
2023-06-02 18:36:10 -04:00
committed by GitHub
parent 72fc215641
commit e0e9c3f166
15 changed files with 398 additions and 88 deletions

View File

@@ -1,6 +1,6 @@
//! Authentication flow interface
use crate::{launcher::auth as inner, State};
use futures::prelude::*;
use chrono::Utc;
use tokio::sync::oneshot;
use crate::state::AuthTask;
@@ -71,22 +71,20 @@ pub async fn refresh(user: uuid::Uuid) -> crate::Result<Credentials> {
let state = State::get().await?;
let mut users = state.users.write().await;
let fetch_semaphore = &state.fetch_semaphore;
futures::future::ready(users.get(user).ok_or_else(|| {
crate::ErrorKind::OtherError(format!(
"You are not logged in with a Minecraft account!"
))
let mut credentials = users.get(user).ok_or_else(|| {
crate::ErrorKind::OtherError(
"You are not logged in with a Minecraft account!".to_string(),
)
.as_error()
}))
.and_then(|mut credentials| async move {
if chrono::offset::Utc::now() > credentials.expires {
inner::refresh_credentials(&mut credentials, fetch_semaphore)
.await?;
}
users.insert(&credentials).await?;
Ok(credentials)
})
.await
})?;
let fetch_semaphore = &state.fetch_semaphore;
if Utc::now() > credentials.expires {
inner::refresh_credentials(&mut credentials, fetch_semaphore).await?;
}
users.insert(&credentials).await?;
Ok(credentials)
}
/// Remove a user account from the database