From 399fdecee7885889147fcf4ac46e8329cfb40271 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 27 May 2024 07:37:46 +0200 Subject: [PATCH] fix(web): only refresh if logged in --- web/src/app.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/web/src/app.rs b/web/src/app.rs index b07ddc4..8e51553 100644 --- a/web/src/app.rs +++ b/web/src/app.rs @@ -12,26 +12,28 @@ pub fn App() -> impl IntoView { let (userid, set_userid) = use_cookie::("user_id"); let (config, set_config, _) = use_local_storage::("config"); - spawn_local(async move { - match reqwest::Client::new() - .request(Method::PATCH, format!("{URL_BASE}/auth")) - .json(&serde_json::json!({"token": token.get().unwrap_or_default()})) - .send() - .await - { - Err(e) => tracing::error!("could not refresh token: {e}"), - Ok(res) => match res.error_for_status() { - Err(e) => tracing::error!("server rejected refresh: {e}"), - Ok(doc) => match doc.json::().await { - Err(e) => tracing::error!("failed parsing auth response: {e}"), - Ok(auth) => { - set_token.set(Some(auth.token)); - set_userid.set(Some(auth.user)); - }, + if let Some(tok) = token.get_untracked() { + spawn_local(async move { + match reqwest::Client::new() + .request(Method::PATCH, format!("{URL_BASE}/auth")) + .json(&serde_json::json!({"token": tok})) + .send() + .await + { + Err(e) => tracing::error!("could not refresh token: {e}"), + Ok(res) => match res.error_for_status() { + Err(e) => tracing::error!("server rejected refresh: {e}"), + Ok(doc) => match doc.json::().await { + Err(e) => tracing::error!("failed parsing auth response: {e}"), + Ok(auth) => { + set_token.set(Some(auth.token)); + set_userid.set(Some(auth.user)); + }, + } } } - } - }); + }) + }; let auth = Auth { token, userid }; provide_context(auth);