Rename signup to register and center forms

This commit is contained in:
Tyler Hallada 2023-09-27 00:23:28 -04:00
parent 092a38ad52
commit bea3529e22
8 changed files with 59 additions and 34 deletions

View File

@ -6,6 +6,18 @@ html {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
body {
display: flex;
flex-direction: column;
margin: 8px;
height: calc(100vh - 16px);
}
main#main-content {
flex-grow: 1;
margin: 0px 8px;
}
.htmx-indicator {
display: none;
}
@ -64,7 +76,7 @@ header.header nav .auth {
footer.footer {
text-align: center;
margin-top: 64px;
margin-bottom: 16px;
margin-bottom: 8px;
}
footer.footer hr {
@ -222,7 +234,16 @@ header.feed-header button {
margin-left: 24px;
}
/* Signup & Login */
/* Register & Login */
.center-horizontal {
width: fit-content;
margin: 0px auto;
}
.center-text {
text-align: center;
}
.auth-form-grid {
display: grid;

View File

@ -27,10 +27,12 @@ pub async fn get(hx_boosted: Option<TypedHeader<HXBoosted>>, layout: Layout) ->
.with_subtitle("login")
.boosted(hx_boosted)
.render(html! {
header {
div class="center-horizontal" {
header class="center-text" {
h2 { "Login" }
}
(login_form(LoginFormProps::default()))
}
}))
}

View File

@ -8,4 +8,4 @@ pub mod feeds;
pub mod log;
pub mod login;
pub mod logout;
pub mod signup;
pub mod register;

View File

@ -10,11 +10,11 @@ use crate::error::{Error, Result};
use crate::htmx::{HXBoosted, HXRedirect};
use crate::models::user::{AuthContext, CreateUser, User};
use crate::partials::layout::Layout;
use crate::partials::signup_form::{signup_form, SignupFormProps};
use crate::partials::register_form::{register_form, RegisterFormProps};
#[serde_as]
#[derive(Debug, Deserialize)]
pub struct Signup {
pub struct Register {
pub email: String,
pub password: String,
pub password_confirmation: String,
@ -24,26 +24,28 @@ pub struct Signup {
pub async fn get(hx_boosted: Option<TypedHeader<HXBoosted>>, layout: Layout) -> Result<Response> {
Ok(layout
.with_subtitle("signup")
.with_subtitle("register")
.boosted(hx_boosted)
.render(html! {
header {
h2 { "Signup" }
div class="center-horizontal" {
header class="center-text" {
h2 { "Register" }
}
(register_form(RegisterFormProps::default()))
}
(signup_form(SignupFormProps::default()))
}))
}
pub async fn post(
State(pool): State<PgPool>,
mut auth: AuthContext,
Form(signup): Form<Signup>,
Form(register): Form<Register>,
) -> Result<Response> {
if signup.password != signup.password_confirmation {
if register.password != register.password_confirmation {
// return Err(Error::BadRequest("passwords do not match"));
return Ok(signup_form(SignupFormProps {
email: Some(signup.email),
name: signup.name,
return Ok(register_form(RegisterFormProps {
email: Some(register.email),
name: register.name,
password_error: Some("passwords do not match".to_string()),
..Default::default()
})
@ -52,9 +54,9 @@ pub async fn post(
let user = match User::create(
&pool,
CreateUser {
email: signup.email.clone(),
password: signup.password.clone(),
name: signup.name.clone(),
email: register.email.clone(),
password: register.password.clone(),
name: register.name.clone(),
},
)
.await
@ -65,9 +67,9 @@ pub async fn post(
let field_errors = validation_errors.field_errors();
dbg!(&validation_errors);
dbg!(&field_errors);
return Ok(signup_form(SignupFormProps {
email: Some(signup.email),
name: signup.name,
return Ok(register_form(RegisterFormProps {
email: Some(register.email),
name: register.name,
email_error: field_errors.get("email").map(|&errors| {
errors
.iter()
@ -96,9 +98,9 @@ pub async fn post(
if let Error::Sqlx(sqlx::error::Error::Database(db_error)) = &err {
if let Some(constraint) = db_error.constraint() {
if constraint == "users_email_idx" {
return Ok(signup_form(SignupFormProps {
email: Some(signup.email),
name: signup.name,
return Ok(register_form(RegisterFormProps {
email: Some(register.email),
name: register.name,
email_error: Some("email already exists".to_string()),
..Default::default()
})

View File

@ -115,8 +115,8 @@ async fn main() -> Result<()> {
.route("/login", get(handlers::login::get))
.route("/login", post(handlers::login::post))
.route("/logout", get(handlers::logout::get))
.route("/signup", get(handlers::signup::get))
.route("/signup", post(handlers::signup::post))
.route("/register", get(handlers::register::get))
.route("/register", post(handlers::register::post))
.nest_service("/static", ServeDir::new("static"))
.with_state(AppState {
pool,

View File

@ -20,7 +20,7 @@ pub fn header(title: &str, user: Option<User>) -> Markup {
} @else {
a href="/login" { "login" }
span { " | " }
a href="/signup" { "signup" }
a href="/register" { "register" }
}
}
}

View File

@ -8,5 +8,5 @@ pub mod header;
pub mod layout;
pub mod login_form;
pub mod opml_import_form;
pub mod signup_form;
pub mod register_form;
pub mod user_name;

View File

@ -1,7 +1,7 @@
use maud::{html, Markup, PreEscaped};
#[derive(Debug, Default)]
pub struct SignupFormProps {
pub struct RegisterFormProps {
pub email: Option<String>,
pub name: Option<String>,
pub email_error: Option<String>,
@ -10,8 +10,8 @@ pub struct SignupFormProps {
pub general_error: Option<String>,
}
pub fn signup_form(props: SignupFormProps) -> Markup {
let SignupFormProps {
pub fn register_form(props: RegisterFormProps) -> Markup {
let RegisterFormProps {
email,
name,
email_error,
@ -20,7 +20,7 @@ pub fn signup_form(props: SignupFormProps) -> Markup {
general_error,
} = props;
html! {
form hx-post="/signup" hx-swap="outerHTML" class="auth-form-grid" {
form hx-post="/register" hx-swap="outerHTML" class="auth-form-grid" {
label for="email" { "Email *" }
input type="email" name="email" id="email" placeholder="Email" value=(email.unwrap_or_default()) required;
@if let Some(email_error) = email_error {