Compare commits
No commits in common. "399fdecee7885889147fcf4ac46e8329cfb40271" and "318fa4f670016274a4a9a89e4aad031050c995e3" have entirely different histories.
399fdecee7
...
318fa4f670
2 changed files with 11 additions and 31 deletions
|
@ -1,39 +1,19 @@
|
|||
use leptos::*;
|
||||
use leptos_router::*;
|
||||
use reqwest::Method;
|
||||
use crate::prelude::*;
|
||||
|
||||
use leptos_use::{storage::use_local_storage, use_cookie, utils::{FromToStringCodec, JsonCodec}};
|
||||
use leptos_use::{storage::use_local_storage, use_cookie, use_cookie_with_options, utils::{FromToStringCodec, JsonCodec}, UseCookieOptions};
|
||||
|
||||
|
||||
#[component]
|
||||
pub fn App() -> impl IntoView {
|
||||
let (token, set_token) = use_cookie::<String, FromToStringCodec>("token");
|
||||
let (userid, set_userid) = use_cookie::<String, FromToStringCodec>("user_id");
|
||||
let (token, set_token) = use_cookie_with_options::<String, FromToStringCodec>(
|
||||
"token",
|
||||
UseCookieOptions::default()
|
||||
.max_age(1000 * 60 * 60 * 6)
|
||||
);
|
||||
let (config, set_config, _) = use_local_storage::<crate::Config, JsonCodec>("config");
|
||||
|
||||
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 (userid, set_userid) = use_cookie::<String, FromToStringCodec>("user_id");
|
||||
|
||||
let auth = Auth { token, userid };
|
||||
provide_context(auth);
|
||||
|
|
|
@ -91,8 +91,8 @@ struct LoginForm {
|
|||
|
||||
|
||||
#[derive(Debug, Clone, serde::Deserialize)]
|
||||
pub struct AuthResponse {
|
||||
pub token: String,
|
||||
pub user: String,
|
||||
pub expires: chrono::DateTime<chrono::Utc>,
|
||||
struct AuthResponse {
|
||||
token: String,
|
||||
user: String,
|
||||
expires: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue