#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
use models::User;
use rocket::{
request::{FlashMessage, Form},
response::{Flash, Redirect},
Route,
};
use utils::{DbConn, Page};
#[derive(BartDisplay)]
#[template = "templates/login.html"]
pub struct LoginTemplate;
implement_responder_for!(LoginTemplate);
#[get("/", format = "text/html")]
pub fn new(flash: Option<FlashMessage>) -> Page<LoginTemplate> {
Page {
title: String::from("Log in"),
flash,
content: LoginTemplate {},
}
}
#[derive(FromForm)]
pub struct LoginForm {
email: String,
password: String,
}
#[post("/create", data = "<login>")]
pub fn create(login: Form<LoginForm>, conn: DbConn) -> Flash<Redirect> {
let login = login.get();
if let Ok(user) = User::by_email(&login.email, &conn) {
if user.password == login.password {
return Flash::success(
Redirect::to("/"),
format!("{} logged in successfully", user.displayname()),
);
}
}
Flash::error(Redirect::to("/login"), "Invalid email or passoword!")
}
pub fn routes() -> Vec<Route> {
routes![new, create]
}