aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/account/mod.rs
blob: 7e329a15144bf01b490cf327746b867def116f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::HtmlTemplate;
use crate::database::User;
use crate::{AppState, CONF};
use rocket::form::Form;
use rocket::{get, post, FromForm, State};

#[derive(FromForm)]
pub struct RegisterForm {
    #[field(validate = len(8..32))]
    pub invitation: String,
    #[field(validate = len(4..32))]
    pub username: String,
    #[field(validate = len(4..64))]
    pub password: String,
}

#[get("/account/register")]
pub fn r_account_register() -> HtmlTemplate<markup::DynRender<'static>> {
    HtmlTemplate(
        "Register".to_string(),
        markup::new! {
            h1 { "Register for " @CONF.brand  }
            form[method="POST", action=""] {
                label[for="inp-invitation"] { "Invite Code: " }
                input[type="text", id="inp-invitation", name="invitation"]; br;

                label[for="inp-username"] { "Username: " }
                input[type="text", id="inp-username", name="username"]; br;
                label[for="inp-password"] { "Password: " }
                input[type="password", id="inp-password", name="password"]; br;

                input[type="submit", value="Register now!"];
            }
        },
    )
}

#[get("/account/login")]
pub fn r_account_login() -> HtmlTemplate<markup::DynRender<'static>> {
    HtmlTemplate(
        "Log in".to_string(),
        markup::new! {
            h1 { "Log in to your Account" }

        },
    )
}

#[post("/account/register", data = "<form>")]
pub fn r_account_register_post(
    state: &State<AppState>,
    form: Form<RegisterForm>,
) -> HtmlTemplate<markup::DynRender<'static>> {
    state
        .database
        .users
        .insert(
            &form.username,
            &User {
                display_name: form.username.clone(),
                name: form.username.clone(),
                password: form.password.clone().into(), // TODO hash it
            },
        )
        .unwrap();
    HtmlTemplate(
        "Registration successful".to_string(),
        markup::new! {
            h1 { "Registration successful." }
        },
    )
}