aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-01-20 15:46:44 +0100
committerHarald Eilertsen <haraldei@anduin.net>2018-01-20 15:46:44 +0100
commit8a30e06af2359917a671b389b13194f455965b03 (patch)
tree1a7e847da1a48a7d0ff2dc64a6b060d9ecd6315d
parent178a9f444b5250a5b76d3d1a98297da8572a05e5 (diff)
downloadrocket-blog-8a30e06af2359917a671b389b13194f455965b03.tar.gz
rocket-blog-8a30e06af2359917a671b389b13194f455965b03.tar.bz2
rocket-blog-8a30e06af2359917a671b389b13194f455965b03.zip
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.
-rw-r--r--src/controllers/login_controller.rs38
-rw-r--r--src/controllers/mod.rs1
-rw-r--r--src/main.rs7
-rw-r--r--src/models/user.rs6
-rw-r--r--templates/login.html15
5 files changed, 66 insertions, 1 deletions
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<rocket::request::FlashMessage>) -> utils::Page<LoginTemplate> {
+ 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="<login>")]
+pub fn create(login: Form<LoginForm>, conn: utils::DbConn) -> Flash<Redirect> {
+ 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<User> {
+ use schema::users::dsl::*;
+ users.filter(email.eq(user_email))
+ .get_result::<User>(&*conn)
+ }
+
pub fn create(new_user: &NewUser, conn: utils::DbConn) -> QueryResult<User> {
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 @@
+<form name="login" id="login" method="post" action="/login/create">
+ <div class="field string required">
+ <label for="login_email">Email:</label>
+ <input type="text" id="login_email" name="email">
+ </div>
+
+ <div class="field password required">
+ <label for="login_password">Password:</label>
+ <input type="password" id="login_password" name="password">
+ </div>
+
+ <div class="actions">
+ <input type="submit" value="Log in...">
+ </div>
+</form>