add flash

This commit is contained in:
philipp 2023-02-16 11:59:01 +01:00
parent 6e54b29288
commit 306ae13467
3 changed files with 59 additions and 23 deletions

BIN
db.sqlite

Binary file not shown.

View File

@ -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("/?<all>")]
async fn index(db: &State<DatabaseConnection>, user: user::Model, all: Option<String>) -> Template {
async fn index(
db: &State<DatabaseConnection>,
user: user::Model,
all: Option<String>,
flash: Option<FlashMessage<'_>>,
) -> Template {
let mut data = Vec::new();
let mut show_next_n_days = 6;
@ -56,12 +62,24 @@ async fn index(db: &State<DatabaseConnection>, user: user::Model, all: Option<St
data.push(DayWithTrips::new(day, db.inner()).await);
}
Template::render("index", context! { data, user })
let mut context = tera::Context::new();
if let Some(msg) = flash {
context.insert("flash", &msg.into_inner());
}
context.insert("data", &data);
context.insert("user", &user);
Template::render("index", &context.into_json())
}
#[get("/name")]
fn name() -> Template {
Template::render("name", context! {})
fn name(flash: Option<FlashMessage<'_>>) -> 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<NameForm>,
cookies: &CookieJar<'_>,
db: &State<DatabaseConnection>,
) -> Redirect {
) -> Flash<Redirect> {
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")]

View File

@ -43,6 +43,25 @@
{% endif %}
<a href="/logout">LOGOUT</a>
{% endif %}
{% if flash %}
{% if flash.0 == "success" %}
<div class="row">
<div class="one-column" style="padding: 15px; background-color: #2b8c68; margin-bottom: 1.6em;">
{{ flash.1 }}
</div>
</div>
{% endif %}
{% if flash.0 == "error" %}
<div class="row">
<div class="one-column" style="padding: 15px; background-color: #ff6961; margin-bottom: 1.6em;">
{{ flash.1 }}
</div>
</div>
{% endif %}
{% endif %}
<div class="row">
<div class="column">
{% block content %}