From e83226ae854080f76e1cbc2b1b337e6c397d04c2 Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 18 May 2018 21:38:43 +0100 Subject: [PATCH] Validate data before creating a new account Fix #28 --- .gitignore | 1 + src/routes/user.rs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 70e3cae7..8f38f71c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +rls /target **/*.rs.bk diff --git a/src/routes/user.rs b/src/routes/user.rs index 8e3a93ce..ae1e0da0 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -125,11 +125,17 @@ struct NewUserForm { } #[post("/users/new", data = "")] -fn create(conn: DbConn, data: Form) -> Redirect { +fn create(conn: DbConn, data: Form) -> Result { let inst = Instance::get_local(&*conn).unwrap(); let form = data.get(); - if form.password == form.password_confirmation { + if form.username.clone().len() < 1 { + Err(String::from("Username is required")) + } else if form.email.clone().len() < 1 { + Err(String::from("Email is required")) + } else if form.password.clone().len() < 8 { + Err(String::from("Password should be at least 8 characters long")) + } else if form.password == form.password_confirmation { User::insert(&*conn, NewUser::new_local( form.username.to_string(), form.username.to_string(), @@ -139,9 +145,10 @@ fn create(conn: DbConn, data: Form) -> Redirect { User::hash_pass(form.password.to_string()), inst.id )).update_boxes(&*conn); + Ok(Redirect::to(format!("/@/{}", data.get().username).as_str())) + } else { + Err(String::from("Passwords don't match")) } - - Redirect::to(format!("/@/{}", data.get().username).as_str()) } #[get("/@//outbox")]