pub mod admin; pub mod session; use super::error::MyError; use super::layout::LayoutPage; use crate::database::Database; use crate::database::User; use crate::routes::ui::error::MyResult; use crate::routes::ui::home::rocket_uri_macro_r_home; use crate::routes::ui::layout::DynLayoutPage; use crate::CONF; use anyhow::anyhow; use argon2::{Argon2, PasswordHasher}; use rocket::form::Contextual; use rocket::form::Form; use rocket::http::{Cookie, CookieJar}; use rocket::response::Redirect; use rocket::{get, post, uri, FromForm, State}; #[derive(FromForm)] pub struct RegisterForm { #[field(validate = len(8..128))] pub invitation: String, #[field(validate = len(4..32))] pub username: String, #[field(validate = len(4..64))] pub password: String, } #[get("/account/register")] pub async fn r_account_register() -> DynLayoutPage<'static> { LayoutPage { title: "Register".to_string(), content: markup::new! { form.account[method="POST", action=""] { h1 { "Register for " @CONF.brand } 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!"]; } }, } } #[derive(FromForm)] pub struct LoginForm { #[field(validate = len(4..32))] pub username: String, #[field(validate = len(..64))] pub password: String, } #[get("/account/login")] pub fn r_account_login() -> DynLayoutPage<'static> { LayoutPage { title: "Log in".to_string(), content: markup::new! { form.account[method="POST", action=""] { h1 { "Log in to your Account" } 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="Login"]; p { "While logged in, a cookie will be used to identify you." } } }, } } #[get("/account/logout")] pub fn r_account_logout() -> DynLayoutPage<'static> { LayoutPage { title: "Log out".to_string(), content: markup::new! { form.account[method="POST", action=""] { h1 { "Log out" } input[type="submit", value="Log out."]; } }, } } #[post("/account/register", data = "