diff --git a/db.sqlite b/db.sqlite index 04ccdc8..3f77359 100644 Binary files a/db.sqlite and b/db.sqlite differ diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 671be62..770a77c 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -9,10 +9,11 @@ use rocket::{ form::{self, Form, ValueField}, fs::FileServer, http::{Cookie, CookieJar}, - response::Redirect, + request::FlashMessage, + response::{Flash, Redirect}, Build, Rocket, State, }; -use rocket_dyn_templates::{context, Template}; +use rocket_dyn_templates::{tera, Template}; use sea_orm::{Database, DatabaseConnection}; use sha3::{Digest, Sha3_256}; @@ -37,7 +38,12 @@ impl Deref for NaiveDateForm { } #[get("/?")] -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 %}