fix(web): only refresh if logged in
This commit is contained in:
parent
dfbac324f5
commit
399fdecee7
1 changed files with 20 additions and 18 deletions
|
@ -12,26 +12,28 @@ pub fn App() -> impl IntoView {
|
|||
let (userid, set_userid) = use_cookie::<String, FromToStringCodec>("user_id");
|
||||
let (config, set_config, _) = use_local_storage::<crate::Config, JsonCodec>("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::<AuthResponse>().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::<AuthResponse>().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);
|
||||
|
|
Loading…
Reference in a new issue