From 306ae13467d848af2a8422c4fa0b662043bfb296 Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 16 Feb 2023 11:59:01 +0100 Subject: [PATCH] add flash --- db.sqlite | Bin 57344 -> 57344 bytes src/rest/mod.rs | 63 +++++++++++++++++++++++++-------------- templates/base.html.tera | 19 ++++++++++++ 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/db.sqlite b/db.sqlite index 04ccdc829c4db38d8928187a6db0ce867da5191b..3f773596d694802be950458dbc00cf49cbbb968e 100644 GIT binary patch delta 92 zcmV-i0HgnazypB51CSd5tdSf;0j#lLpI;LN4FCWD1rIy`53mnl4^")] -async fn index(db: &State, user: user::Model, all: Option) -> Template { +async fn index( + db: &State, + user: user::Model, + all: Option, + flash: Option>, +) -> Template { let mut data = Vec::new(); let mut show_next_n_days = 6; @@ -56,12 +62,24 @@ async fn index(db: &State, user: user::Model, all: Option Template { - Template::render("name", context! {}) +fn name(flash: Option>) -> Template { + let mut context = tera::Context::new(); + + if let Some(msg) = flash { + context.insert("flash", &msg.into_inner()); + } + Template::render("name", &context.into_json()) } #[derive(FromForm)] @@ -75,32 +93,31 @@ async fn savename( name: Form, cookies: &CookieJar<'_>, db: &State, -) -> Redirect { +) -> Flash { let user = user::Model::find_or_create_user(&name.name, db.inner()).await; match user.pw { - Some(pw) => { - match &name.pw { - Some(entered_pw) => { - let mut hasher = Sha3_256::new(); - hasher.update(entered_pw); - let entered_pw = hasher.finalize(); + Some(pw) => match &name.pw { + Some(entered_pw) => { + let mut hasher = Sha3_256::new(); + hasher.update(entered_pw); + let entered_pw = hasher.finalize(); - if hex::encode(entered_pw) == pw { - cookies.add_private(Cookie::new("name", name.name.clone())); - } else { - Redirect::to("/"); // Wrong PW - } - } - None => { - Redirect::to("/"); // No PW supplied + if hex::encode(entered_pw) == pw { + cookies.add_private(Cookie::new("name", name.name.clone())); + return Flash::success(Redirect::to("/"), "Erfolgreich eingeloggt"); + } else { + return Flash::error(Redirect::to("/name"), "Falsches Passwort"); } } - } + None => { + return Flash::error(Redirect::to("/name"), "Benutzer besitzt hat Passwort, du hast jedoch keines eingegeben. Bitte nochmal probieren"); + } + }, None => { cookies.add_private(Cookie::new("name", name.name.clone())); + return Flash::success(Redirect::to("/"), "Name erfolgreich ausgewählt"); } } - Redirect::to("/") } #[get("/logout")] diff --git a/templates/base.html.tera b/templates/base.html.tera index bedfac8..4a4a080 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -43,6 +43,25 @@ {% endif %} LOGOUT {% endif %} + + {% if flash %} + {% if flash.0 == "success" %} +
+
+ {{ flash.1 }} +
+
+ {% endif %} + {% if flash.0 == "error" %} +
+
+ {{ flash.1 }} +
+
+ {% endif %} + {% endif %} + +
{% block content %}