1
0
Fork 0

Add user validation

This commit is contained in:
Alex Kotov 2020-10-15 04:53:24 +05:00
parent f27b5e2d21
commit fcbb6f1598
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 29 additions and 4 deletions

11
src/forms.rs Normal file
View File

@ -0,0 +1,11 @@
#[derive(FromForm)]
pub struct UserSignUp {
pub username: String,
pub password: String,
}
impl UserSignUp {
pub fn is_valid(&self) -> bool {
true
}
}

View File

@ -5,6 +5,7 @@ mod database;
mod routes;
mod schema;
mod models;
mod forms;
#[macro_use] extern crate diesel;
#[macro_use] extern crate rocket;

View File

@ -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(),
};