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, 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!") } pub fn routes() -> Vec { routes![new, create] }