feat(web): get my username from login

This commit is contained in:
əlemi 2024-04-15 22:32:05 +02:00
parent 3cf401467e
commit 1cf170c16a
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 15 additions and 4 deletions

View file

@ -27,3 +27,4 @@ apb = { path = "../apb", features = ["unstructured"] }
futures = "0.3.30" futures = "0.3.30"
dissolve = "0.2" # TODO strip html without this crate dissolve = "0.2" # TODO strip html without this crate
lazy_static = "1.4" lazy_static = "1.4"
chrono = { version = "0.4", features = ["serde"] }

View file

@ -17,7 +17,7 @@ struct LoginForm {
fn web_uri(kind: &str, url: &str) -> String { fn web_uri(kind: &str, url: &str) -> String {
if url.starts_with(URL_BASE) { if url.starts_with(URL_BASE) {
format!("/web/{kind}/{}", url.split('/').last().unwrap_or_default().to_string()) format!("/web/{kind}/{}", url.split('/').last().unwrap_or_default())
} else { } else {
format!("/web/{kind}/+{}", url.replace("https://", "").replace('/', "@")) format!("/web/{kind}/+{}", url.replace("https://", "").replace('/', "@"))
} }
@ -31,17 +31,25 @@ fn api_uri(kind: &str, url: &str) -> String {
} }
} }
#[derive(Debug, serde::Deserialize)]
struct AuthSuccess {
token: String,
user: String,
expires: chrono::DateTime<chrono::Utc>,
}
#[component] #[component]
pub fn LoginBox( pub fn LoginBox(
rx: Signal<Option<String>>, rx: Signal<Option<String>>,
tx: WriteSignal<Option<String>>, tx: WriteSignal<Option<String>>,
) -> impl IntoView { ) -> impl IntoView {
let (username, username_set) = create_signal("".to_string());
let username_ref: NodeRef<html::Input> = create_node_ref(); let username_ref: NodeRef<html::Input> = create_node_ref();
let password_ref: NodeRef<html::Input> = create_node_ref(); let password_ref: NodeRef<html::Input> = create_node_ref();
view! { view! {
<div> <div>
<div class="w-100" class:hidden=move || { rx.get().unwrap_or_default().is_empty() }> <div class="w-100" class:hidden=move || { rx.get().unwrap_or_default().is_empty() }>
"Hello "<a href="/web/users/test" >test</a> "Hello "<a href={move || web_uri("users", &username.get())} >{move || username.get()}</a>
<input style="float:right" type="submit" value="logout" on:click=move |_| { <input style="float:right" type="submit" value="logout" on:click=move |_| {
tx.set(None); tx.set(None);
} /> } />
@ -59,9 +67,11 @@ pub fn LoginBox(
.json(&LoginForm { email, password }) .json(&LoginForm { email, password })
.send() .send()
.await.unwrap() .await.unwrap()
.json::<String>() .json::<AuthSuccess>()
.await.unwrap(); .await.unwrap();
tx.set(Some(auth)); tx.set(Some(auth.token));
username_set.set(auth.user);
console_log(&format!("logged in until {}", auth.expires));
}); });
} /> } />
</div> </div>