2024-02-14 20:33:07 +00:00
|
|
|
//! Implementing a login form
|
|
|
|
//!
|
|
|
|
//! This example demonstrates how to implement a login form using Dioxus desktop. Since forms typically navigate the
|
|
|
|
//! page on submit, we need to intercept the onsubmit event and send a request to a server. On the web, we could
|
|
|
|
//! just leave the submit action` as is, but on desktop, we need to handle the form submission ourselves.
|
|
|
|
//!
|
|
|
|
//! Todo: actually spin up a server and run the login flow. Login is way more complex than a form override :)
|
2022-01-08 02:20:52 +00:00
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2024-05-28 20:05:55 +00:00
|
|
|
launch(app);
|
2022-01-08 02:20:52 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2023-10-23 20:26:10 +00:00
|
|
|
let onsubmit = move |evt: FormEvent| async move {
|
|
|
|
let resp = reqwest::Client::new()
|
|
|
|
.post("http://localhost:8080/login")
|
|
|
|
.form(&[
|
2024-01-05 01:02:00 +00:00
|
|
|
("username", &evt.values()["username"]),
|
|
|
|
("password", &evt.values()["password"]),
|
2023-10-23 20:26:10 +00:00
|
|
|
])
|
|
|
|
.send()
|
|
|
|
.await;
|
2022-01-08 02:20:52 +00:00
|
|
|
|
2023-10-23 20:26:10 +00:00
|
|
|
match resp {
|
|
|
|
// Parse data from here, such as storing a response token
|
|
|
|
Ok(_data) => println!("Login successful!"),
|
2022-01-08 02:20:52 +00:00
|
|
|
|
2023-10-23 20:26:10 +00:00
|
|
|
//Handle any errors from the fetch here
|
|
|
|
Err(_err) => {
|
|
|
|
println!("Login failed - you need a login server running on localhost:8080.")
|
2022-01-08 02:20:52 +00:00
|
|
|
}
|
2023-10-23 20:26:10 +00:00
|
|
|
}
|
2022-01-08 02:20:52 +00:00
|
|
|
};
|
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2022-01-08 02:20:52 +00:00
|
|
|
h1 { "Login" }
|
2024-01-16 05:12:44 +00:00
|
|
|
form { onsubmit,
|
2022-11-16 00:37:23 +00:00
|
|
|
input { r#type: "text", id: "username", name: "username" }
|
2022-02-16 19:08:57 +00:00
|
|
|
label { "Username" }
|
2022-01-08 02:20:52 +00:00
|
|
|
br {}
|
2022-11-16 00:37:23 +00:00
|
|
|
input { r#type: "password", id: "password", name: "password" }
|
2022-02-16 19:11:31 +00:00
|
|
|
label { "Password" }
|
2022-01-08 02:20:52 +00:00
|
|
|
br {}
|
2022-02-16 19:08:57 +00:00
|
|
|
button { "Login" }
|
2022-01-08 02:20:52 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-02-16 19:08:57 +00:00
|
|
|
}
|