From 2fde47d909a1a2aa01f8ccfb36c559dcc5c15d52 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Mon, 4 Jun 2018 20:21:43 +0200 Subject: [PATCH] add optional login message and callback --- src/main.rs | 1 + src/routes/session.rs | 24 ++++++++++++++++++++---- templates/session/login.tera | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7b6b803..2c50b40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,6 +94,7 @@ fn main() { routes::reshares::create, routes::session::new, + routes::session::new_message, routes::session::create, routes::session::delete, diff --git a/src/routes/session.rs b/src/routes/session.rs index f8a1ed9..bf7d5e6 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -1,7 +1,7 @@ use rocket::{ http::{Cookie, Cookies}, response::{Redirect, status::NotFound}, - request::Form + request::{Form,FlashMessage} }; use rocket_contrib::Template; @@ -15,6 +15,20 @@ fn new(user: Option) -> Template { })) } +#[derive(FromForm)] +struct Message { + m: String +} + +#[get("/login?")] +fn new_message(user: Option, message: Message) -> Template { + Template::render("session/login", json!({ + "account": user, + "message": message.m + })) +} + + #[derive(FromForm)] struct LoginForm { email_or_name: String, @@ -22,7 +36,7 @@ struct LoginForm { } #[post("/login", data = "")] -fn create(conn: DbConn, data: Form, mut cookies: Cookies) -> Result> { +fn create(conn: DbConn, data: Form, flash: Option, mut cookies: Cookies) -> Result> { let form = data.get(); let user = match User::find_by_email(&*conn, form.email_or_name.to_string()) { Some(usr) => Ok(usr), @@ -31,12 +45,14 @@ fn create(conn: DbConn, data: Form, mut cookies: Cookies) -> Result Err("Invalid username or password") } }; - match user { Ok(usr) => { if usr.auth(form.password.to_string()) { cookies.add_private(Cookie::new(AUTH_COOKIE, usr.id.to_string())); - Ok(Redirect::to("/")) + Ok(Redirect::to(&flash + .and_then(|f| if f.name()=="callback" { Some(f.msg().to_owned()) } else { None }) + .unwrap_or("/".to_owned())) + ) } else { Err(NotFound(String::from("Invalid username or password"))) } diff --git a/templates/session/login.tera b/templates/session/login.tera index eb2cebe..1099b5e 100644 --- a/templates/session/login.tera +++ b/templates/session/login.tera @@ -6,6 +6,9 @@ Login {% block content %}

Login

+{% if message %} +

{{ message | escape }}

+{% endif %}