login_form.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! Implementing a login form
  2. //!
  3. //! This example demonstrates how to implement a login form using Dioxus desktop. Since forms typically navigate the
  4. //! page on submit, we need to intercept the onsubmit event and send a request to a server. On the web, we could
  5. //! just leave the `submit action` as is, but on desktop, we need to handle the form submission ourselves.
  6. //!
  7. //! Todo: actually spin up a server and run the login flow. Login is way more complex than a form override :)
  8. use dioxus::prelude::*;
  9. fn main() {
  10. dioxus::launch(app);
  11. }
  12. fn app() -> Element {
  13. let onsubmit = move |evt: FormEvent| async move {
  14. let resp = reqwest::Client::new()
  15. .post("http://localhost:8080/login")
  16. .form(&[
  17. ("username", &evt.values()["username"]),
  18. ("password", &evt.values()["password"]),
  19. ])
  20. .send()
  21. .await;
  22. match resp {
  23. // Parse data from here, such as storing a response token
  24. Ok(_data) => println!("Login successful!"),
  25. //Handle any errors from the fetch here
  26. Err(_err) => {
  27. println!("Login failed - you need a login server running on localhost:8080.")
  28. }
  29. }
  30. };
  31. rsx! {
  32. h1 { "Login" }
  33. form { onsubmit,
  34. input { r#type: "text", id: "username", name: "username" }
  35. label { "Username" }
  36. br {}
  37. input { r#type: "password", id: "password", name: "password" }
  38. label { "Password" }
  39. br {}
  40. button { "Login" }
  41. }
  42. }
  43. }