login_form.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //! This example demonstrates the following:
  2. //! Futures in a callback, Router, and Forms
  3. use dioxus::events::*;
  4. use dioxus::prelude::*;
  5. fn main() {
  6. dioxus_desktop::launch(app);
  7. }
  8. fn app(cx: Scope) -> Element {
  9. let onsubmit = move |evt: FormEvent| {
  10. cx.spawn(async move {
  11. let resp = reqwest::Client::new()
  12. .post("http://localhost:8080/login")
  13. .form(&[
  14. ("username", &evt.values["username"]),
  15. ("password", &evt.values["password"]),
  16. ])
  17. .send()
  18. .await;
  19. match resp {
  20. // Parse data from here, such as storing a response token
  21. Ok(_data) => println!("Login successful!"),
  22. //Handle any errors from the fetch here
  23. Err(_err) => {
  24. println!("Login failed - you need a login server running on localhost:8080.")
  25. }
  26. }
  27. });
  28. };
  29. cx.render(rsx! {
  30. h1 { "Login" }
  31. form {
  32. onsubmit: onsubmit,
  33. prevent_default: "onsubmit", // Prevent the default behavior of <form> to post
  34. input { "type": "text", id: "username", name: "username" }
  35. label { "Username" }
  36. br {}
  37. input { "type": "password", id: "password", name: "password" }
  38. label { "Password" }
  39. br {}
  40. button { "Login" }
  41. }
  42. })
  43. }