Add user validation
This commit is contained in:
parent
f27b5e2d21
commit
fcbb6f1598
|
@ -0,0 +1,11 @@
|
|||
#[derive(FromForm)]
|
||||
pub struct UserSignUp {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl UserSignUp {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ mod database;
|
|||
mod routes;
|
||||
mod schema;
|
||||
mod models;
|
||||
mod forms;
|
||||
|
||||
#[macro_use] extern crate diesel;
|
||||
#[macro_use] extern crate rocket;
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use crate::database;
|
||||
use crate::schema;
|
||||
use crate::models;
|
||||
use crate::forms;
|
||||
|
||||
use diesel::prelude::*;
|
||||
use rocket::response::Redirect;
|
||||
use rocket::request::Form;
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
#[derive(Serialize)]
|
||||
|
@ -30,14 +32,25 @@ fn index(db_conn: database::DbConn) -> Template {
|
|||
Template::render("index", &template_context)
|
||||
}
|
||||
|
||||
#[post("/users/<username>/<password>")]
|
||||
fn sign_up(db_conn: database::DbConn, username: String, password: String) -> Redirect {
|
||||
#[post("/users", data = "<user_sign_up_form>")]
|
||||
fn sign_up(
|
||||
db_conn: database::DbConn,
|
||||
user_sign_up_form: Form<forms::UserSignUp>,
|
||||
) -> Redirect
|
||||
{
|
||||
use schema::users;
|
||||
|
||||
let encrypted_password = bcrypt::hash(password, bcrypt::DEFAULT_COST).unwrap();
|
||||
if !user_sign_up_form.is_valid() {
|
||||
return Redirect::to(uri!(index));
|
||||
}
|
||||
|
||||
let encrypted_password = bcrypt::hash(
|
||||
user_sign_up_form.password.to_string(),
|
||||
bcrypt::DEFAULT_COST,
|
||||
).unwrap();
|
||||
|
||||
let new_user = models::NewUser {
|
||||
username: username.as_str(),
|
||||
username: user_sign_up_form.username.as_str(),
|
||||
encrypted_password: encrypted_password.as_str(),
|
||||
};
|
||||
|
||||
|
|
Reference in New Issue