From 8a30e06af2359917a671b389b13194f455965b03 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 20 Jan 2018 15:46:44 +0100 Subject: Add simple unfinished login controller. It will not actually log you in yet, but will check email/password and report back if it is good or not. More tbd. --- src/controllers/login_controller.rs | 38 +++++++++++++++++++++++++++++++++++++ src/controllers/mod.rs | 1 + src/main.rs | 7 ++++++- src/models/user.rs | 6 ++++++ templates/login.html | 15 +++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/controllers/login_controller.rs create mode 100644 templates/login.html diff --git a/src/controllers/login_controller.rs b/src/controllers/login_controller.rs new file mode 100644 index 0000000..35841b3 --- /dev/null +++ b/src/controllers/login_controller.rs @@ -0,0 +1,38 @@ +use rocket; +use rocket::request::Form; +use rocket::response::{Flash, Redirect}; +use models; +use utils; + +#[derive(BartDisplay)] +#[template = "templates/login.html"] +pub struct LoginTemplate; + +implement_responder_for!(LoginTemplate); + +#[get("/", format="text/html")] +pub fn new(flash: Option) -> utils::Page { + utils::Page { + title: String::from("Log in"), + flash: flash.map_or(None, |f| Some(f.msg().to_string())), + content: LoginTemplate{} + } +} + +#[derive(FromForm)] +pub struct LoginForm { + email: String, + password: String, +} + +#[post("/create", data="")] +pub fn create(login: Form, conn: utils::DbConn) -> Flash { + let login = login.get(); + if let Ok(user) = models::User::by_email(&login.email, conn) { + if user.password == login.password { + return Flash::success(Redirect::to("/"), format!("{} logged in successfully", user.realname.or(Some(user.username)).unwrap())); + } + } + + Flash::error(Redirect::to("/login"), "Invalid email or passoword!") +} diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 401071a..4613e5f 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -1,3 +1,4 @@ pub mod home_controller; +pub mod login_controller; pub mod posts_controller; pub mod users_controller; diff --git a/src/main.rs b/src/main.rs index 88aa160..6fa552f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ mod models; mod schema; mod controllers; use controllers::home_controller; -use controllers::{posts_controller, users_controller}; +use controllers::{login_controller, posts_controller, users_controller}; fn main() { if let Ok(dburl) = dotenv::var("DATABASE_URL") { @@ -41,6 +41,11 @@ fn main() { users_controller::new, users_controller::create, ]) + .mount("/login", + routes![ + login_controller::new, + login_controller::create, + ]) .launch(); } else { diff --git a/src/models/user.rs b/src/models/user.rs index c3f4f72..5b92f6d 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -29,6 +29,12 @@ impl User { .map(|ref v| v[0].clone()) } + pub fn by_email(user_email: &str, conn: utils::DbConn) -> QueryResult { + use schema::users::dsl::*; + users.filter(email.eq(user_email)) + .get_result::(&*conn) + } + pub fn create(new_user: &NewUser, conn: utils::DbConn) -> QueryResult { use ::schema::users::dsl::*; diesel::insert_into(users) diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..88698e2 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,15 @@ +
+
+ + +
+ +
+ + +
+ +
+ +
+
-- cgit v1.2.3