setup.rs
| 1 | use crate::i18n; |
| 2 | use leptos::prelude::*; |
| 3 | use wasm_bindgen_futures::spawn_local; |
| 4 | |
| 5 | use crate::api::{self, Me, input_value}; |
| 6 | use crate::app::AuthPhase; |
| 7 | use crate::components::logo::Logo; |
| 8 | use crate::router::{Location, navigate}; |
| 9 | |
| 10 | #[component] |
| 11 | pub fn SetupView( |
| 12 | set_me: WriteSignal<Option<Me>>, |
| 13 | set_phase: WriteSignal<AuthPhase>, |
| 14 | ) -> impl IntoView { |
| 15 | let (error, set_error) = signal(String::new()); |
| 16 | let (busy, set_busy) = signal(false); |
| 17 | |
| 18 | let on_submit = move |ev: web_sys::SubmitEvent| { |
| 19 | ev.prevent_default(); |
| 20 | let name = input_value("setup-name"); |
| 21 | let pass = input_value("setup-pass"); |
| 22 | let confirm = input_value("setup-confirm"); |
| 23 | let name = name.trim(); |
| 24 | |
| 25 | // The inputs check the name and the password length. |
| 26 | if pass != confirm { |
| 27 | set_error.set(i18n::t(i18n::k::SETUP_PW_MISMATCH).into()); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | set_error.set(String::new()); |
| 32 | set_busy.set(true); |
| 33 | let (name, pass) = (name.to_string(), pass); |
| 34 | spawn_local(async move { |
| 35 | match api::setup(name, pass).await { |
| 36 | Ok(_) => match api::me().await { |
| 37 | Ok(m) => { |
| 38 | navigate(&Location::root()); |
| 39 | set_me.set(Some(m)); |
| 40 | set_phase.set(AuthPhase::Authed); |
| 41 | } |
| 42 | Err(e) => { |
| 43 | set_error.set(e.to_string()); |
| 44 | set_busy.set(false); |
| 45 | } |
| 46 | }, |
| 47 | Err(e) => { |
| 48 | set_error.set(e.to_string()); |
| 49 | set_busy.set(false); |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | }; |
| 54 | |
| 55 | view! { |
| 56 | <div class="center-screen"> |
| 57 | <div class="card auth-card"> |
| 58 | <Logo class="auth-logo".to_string()/> |
| 59 | <h1>"filebrowser-ng"</h1> |
| 60 | <p class="muted">{i18n::tr(i18n::k::SETUP_INTRO)}</p> |
| 61 | <form on:submit=on_submit> |
| 62 | <label class="field"> |
| 63 | <span>{i18n::tr(i18n::k::NAME)}</span> |
| 64 | <input |
| 65 | id="setup-name" |
| 66 | type="text" |
| 67 | required=true |
| 68 | maxlength="64" |
| 69 | autofocus=true |
| 70 | autocomplete="username" |
| 71 | /> |
| 72 | </label> |
| 73 | <label class="field"> |
| 74 | <span>{i18n::tr(i18n::k::PASSWORD)}</span> |
| 75 | <input |
| 76 | id="setup-pass" |
| 77 | type="password" |
| 78 | required=true |
| 79 | minlength="8" |
| 80 | autocomplete="new-password" |
| 81 | /> |
| 82 | </label> |
| 83 | <label class="field"> |
| 84 | <span>{i18n::tr(i18n::k::REPEAT_PASSWORD)}</span> |
| 85 | <input |
| 86 | id="setup-confirm" |
| 87 | type="password" |
| 88 | autocomplete="new-password" |
| 89 | /> |
| 90 | </label> |
| 91 | <p class="error">{move || error.get()}</p> |
| 92 | <button class="btn-primary" type="submit" disabled=move || busy.get()> |
| 93 | {i18n::tr(i18n::k::CREATE_ADMIN)} |
| 94 | </button> |
| 95 | </form> |
| 96 | </div> |
| 97 | </div> |
| 98 | } |
| 99 | } |
| 100 |