fix(web): only refresh if logged in

This commit is contained in:
əlemi 2024-05-27 07:37:46 +02:00
parent dfbac324f5
commit 399fdecee7
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -12,26 +12,28 @@ pub fn App() -> impl IntoView {
let (userid, set_userid) = use_cookie::<String, FromToStringCodec>("user_id"); let (userid, set_userid) = use_cookie::<String, FromToStringCodec>("user_id");
let (config, set_config, _) = use_local_storage::<crate::Config, JsonCodec>("config"); let (config, set_config, _) = use_local_storage::<crate::Config, JsonCodec>("config");
spawn_local(async move { if let Some(tok) = token.get_untracked() {
match reqwest::Client::new() spawn_local(async move {
.request(Method::PATCH, format!("{URL_BASE}/auth")) match reqwest::Client::new()
.json(&serde_json::json!({"token": token.get().unwrap_or_default()})) .request(Method::PATCH, format!("{URL_BASE}/auth"))
.send() .json(&serde_json::json!({"token": tok}))
.await .send()
{ .await
Err(e) => tracing::error!("could not refresh token: {e}"), {
Ok(res) => match res.error_for_status() { Err(e) => tracing::error!("could not refresh token: {e}"),
Err(e) => tracing::error!("server rejected refresh: {e}"), Ok(res) => match res.error_for_status() {
Ok(doc) => match doc.json::<AuthResponse>().await { Err(e) => tracing::error!("server rejected refresh: {e}"),
Err(e) => tracing::error!("failed parsing auth response: {e}"), Ok(doc) => match doc.json::<AuthResponse>().await {
Ok(auth) => { Err(e) => tracing::error!("failed parsing auth response: {e}"),
set_token.set(Some(auth.token)); Ok(auth) => {
set_userid.set(Some(auth.user)); set_token.set(Some(auth.token));
}, set_userid.set(Some(auth.user));
},
}
} }
} }
} })
}); };
let auth = Auth { token, userid }; let auth = Auth { token, userid };
provide_context(auth); provide_context(auth);