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; 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 { .htmx-indicator {
display: none; display: none;
} }
@ -64,7 +76,7 @@ header.header nav .auth {
footer.footer { footer.footer {
text-align: center; text-align: center;
margin-top: 64px; margin-top: 64px;
margin-bottom: 16px; margin-bottom: 8px;
} }
footer.footer hr { footer.footer hr {
@ -222,7 +234,16 @@ header.feed-header button {
margin-left: 24px; margin-left: 24px;
} }
/* Signup & Login */ /* Register & Login */
.center-horizontal {
width: fit-content;
margin: 0px auto;
}
.center-text {
text-align: center;
}
.auth-form-grid { .auth-form-grid {
display: grid; display: grid;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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