diff --git a/src/routes/sessions.rs b/src/routes/sessions.rs index 7df5294..fd6e963 100644 --- a/src/routes/sessions.rs +++ b/src/routes/sessions.rs @@ -25,6 +25,7 @@ pub fn new( let page_context = views::sessions::New { authenticity_token: csrf_token.0.to_string(), + username: "".to_string(), }; let context = views::Site { @@ -54,10 +55,10 @@ pub fn create( } let user = models::User::by_username(db_conn, form.username.to_string()) - .or_else(|_| Err(invalid_sign_in_credentials(&csrf_token.0)))?; + .or_else(|_| Err(invalid_sign_in_credentials(&csrf_token.0, &form.0)))?; if !user.authorize(&form.password) { - return Err(invalid_sign_in_credentials(&csrf_token.0)); + return Err(invalid_sign_in_credentials(&csrf_token.0, &form.0)); } cookies.add_private(Cookie::new("user_id", user.id.to_string())); @@ -85,9 +86,13 @@ pub fn delete( Ok(Redirect::to(uri!(super::home::index))) } -fn invalid_sign_in_credentials(authenticity_token: &String) -> CommonResponse { +fn invalid_sign_in_credentials( + authenticity_token: &String, + form: &forms::UserSignIn, +) -> CommonResponse { let page_context = views::sessions::New { authenticity_token: authenticity_token.to_string(), + username: form.username.to_string(), }; let context = views::Site { diff --git a/src/routes/users.rs b/src/routes/users.rs index 2525fb0..b7d8723 100644 --- a/src/routes/users.rs +++ b/src/routes/users.rs @@ -25,6 +25,7 @@ pub fn new( let page_context = views::users::New { authenticity_token: csrf_token.0.to_string(), + username: "".to_string(), }; let context = views::Site { @@ -54,18 +55,22 @@ pub fn create( } let user = models::NewUser::from_form(&form) - .or_else(|_| Err(invalid_sign_up_form(&csrf_token.0)))? + .or_else(|_| Err(invalid_sign_up_form(&csrf_token.0, &form.0)))? .save(db_conn) - .or_else(|_| Err(invalid_sign_up_form(&csrf_token.0)))?; + .or_else(|_| Err(invalid_sign_up_form(&csrf_token.0, &form.0)))?; cookies.add_private(Cookie::new("user_id", user.id.to_string())); Ok(Redirect::to(uri!(super::home::index))) } -fn invalid_sign_up_form(authenticity_token: &String) -> CommonResponse { +fn invalid_sign_up_form( + authenticity_token: &String, + form: &forms::UserSignUp, +) -> CommonResponse { let page_context = views::users::New { authenticity_token: authenticity_token.to_string(), + username: form.username.to_string(), }; let context = views::Site { diff --git a/src/views.rs b/src/views.rs index 4073193..454a500 100644 --- a/src/views.rs +++ b/src/views.rs @@ -28,6 +28,7 @@ pub mod sessions { #[derive(Serialize)] pub struct New { pub authenticity_token: String, + pub username: String, } } @@ -35,5 +36,6 @@ pub mod users { #[derive(Serialize)] pub struct New { pub authenticity_token: String, + pub username: String, } } diff --git a/templates/sessions/new.html.hbs b/templates/sessions/new.html.hbs index 0c32c81..6ba2fe6 100644 --- a/templates/sessions/new.html.hbs +++ b/templates/sessions/new.html.hbs @@ -6,12 +6,12 @@