Validate data before creating a new account

Fix #28
This commit is contained in:
Bat 2018-05-18 21:38:43 +01:00
parent 1870f52332
commit e83226ae85
2 changed files with 12 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
rls
/target /target
**/*.rs.bk **/*.rs.bk

View File

@ -125,11 +125,17 @@ struct NewUserForm {
} }
#[post("/users/new", data = "<data>")] #[post("/users/new", data = "<data>")]
fn create(conn: DbConn, data: Form<NewUserForm>) -> Redirect { fn create(conn: DbConn, data: Form<NewUserForm>) -> Result<Redirect, String> {
let inst = Instance::get_local(&*conn).unwrap(); let inst = Instance::get_local(&*conn).unwrap();
let form = data.get(); 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( User::insert(&*conn, NewUser::new_local(
form.username.to_string(), form.username.to_string(),
form.username.to_string(), form.username.to_string(),
@ -139,9 +145,10 @@ fn create(conn: DbConn, data: Form<NewUserForm>) -> Redirect {
User::hash_pass(form.password.to_string()), User::hash_pass(form.password.to_string()),
inst.id inst.id
)).update_boxes(&*conn); )).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("/@/<name>/outbox")] #[get("/@/<name>/outbox")]