User creation now possible

at /register
This commit is contained in:
2026-04-29 19:47:16 +02:00
parent 50c0b99f20
commit aa1c11047e
5 changed files with 153 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
[serve]
# The address to serve on LAN.
addresses = ["127.0.0.1", "Brick"]
addresses = ["127.0.0.1"]
# The address to serve on WAN.
# address = "0.0.0.0"
# The port to serve on.

View File

@@ -13,6 +13,8 @@ enum Route {
TicketById { id: i32 },
#[at("/tickets")]
AllTickets,
#[at("/register")]
Register,
#[not_found]
#[at("/404")]
NotFound,
@@ -25,6 +27,7 @@ fn switch(route: Route) -> Html {
Route::Ticket => html! { <ticket::SubmitTicket/> },
Route::TicketById { id } => html! { <ticket::ShowTicketByID id={id}/> },
Route::AllTickets => html! { <ticket::AllTickets/> },
Route::Register => html! { <user::Register/> },
}
}

View File

@@ -1,2 +1,3 @@
pub mod basic_pages;
pub mod ticket;
pub mod user;

View File

@@ -1,6 +1,5 @@
use gloo_net::http::Request;
use serde::{Deserialize, Serialize};
use serde_json::json;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::spawn_local;
use web_sys::HtmlSelectElement;

148
frontend/src/pages/user.rs Normal file
View File

@@ -0,0 +1,148 @@
use gloo_net::http::Request;
use serde::{Deserialize, Serialize};
use wasm_bindgen_futures::spawn_local;
use yew::prelude::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct User {
pub id: i16,
pub last_name: String,
pub first_name: String,
pub username: String,
pub is_admin: bool,
pub pwd: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct UserCreateScheme {
pub first_name: String,
pub last_name: String,
pub username: String,
pub is_admin: bool,
pub pwd: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LoginScheme {
pub username: String,
pub pwd: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FilteredUser {
pub id: i16,
pub first_name: String,
pub last_name: String,
pub username: String,
pub is_admin: bool,
}
#[component(Register)]
pub fn register_component() -> Html {
let first_name = use_state(|| "".to_string());
let last_name = use_state(|| "".to_string());
let username = use_state(|| "".to_string());
let is_admin = use_state(|| false);
let pwd = use_state(|| "".to_string());
let status = use_state(|| None::<String>);
let onsubmit = {
let first_name = first_name.clone();
let last_name = last_name.clone();
let username = username.clone();
let is_admin = is_admin.clone();
let pwd = pwd.clone();
let status = status.clone();
Callback::from(move |e: SubmitEvent| {
e.prevent_default();
let first_name = (*first_name).clone();
let last_name = (*last_name).clone();
let username = (*username).clone();
let is_admin = *is_admin;
let pwd = (*pwd).clone();
let status = status.clone();
spawn_local(async move {
let payload = UserCreateScheme {
first_name,
last_name,
username,
is_admin,
pwd,
};
let request = Request::post("/api/register")
.json(&payload)
.expect("Error building request");
match request.send().await {
Ok(response) if response.status() == 200 => status.set(Some("Success".into())),
Ok(response) => status.set(Some(format!("Error: {}", response.status()))),
Err(err) => status.set(Some(format!("Network error: {}", err))),
}
});
})
};
let fn_change = {
let first_name = first_name.clone();
Callback::from(move |e: InputEvent| {
let input: web_sys::HtmlInputElement = e.target_unchecked_into();
first_name.set(input.value());
})
};
let ln_change = {
let last_name = last_name.clone();
Callback::from(move |e: InputEvent| {
let input: web_sys::HtmlInputElement = e.target_unchecked_into();
last_name.set(input.value());
})
};
let un_change = {
let username = username.clone();
Callback::from(move |e: InputEvent| {
let input: web_sys::HtmlInputElement = e.target_unchecked_into();
username.set(input.value());
})
};
let admin_change = {
let is_admin = is_admin.clone();
Callback::from(move |e: Event| {
let input: web_sys::HtmlInputElement = e.target_unchecked_into();
is_admin.set(input.checked());
})
};
let pwd_change = {
let pwd = pwd.clone();
Callback::from(move |e: InputEvent| {
let input: web_sys::HtmlInputElement = e.target_unchecked_into();
pwd.set(input.value());
})
};
html! {
<form {onsubmit}>
<label>{ "Vorname:" }
<input type="text" value={(*first_name).clone()} oninput={fn_change}/>
</label>
<label>{ "Nachname:" }
<input type="text" value={(*last_name).clone()} oninput={ln_change}/>
</label>
<label>{ "Benutzername:" }
<input type="text" value={(*username).clone()} oninput={un_change}/>
</label>
<label>{ "Admin:" }
<input type="checkbox" checked={*is_admin} onchange={admin_change}/>
</label>
<label>{ "Password:" }
<input type="password" value={(*pwd).clone()} oninput={pwd_change}/>
</label>
<button type="submit">{ "Bestätigen" }</button>
</form>
}
}