forked from Ruderverein-Donau-Linz/rowt
add flash
This commit is contained in:
parent
6e54b29288
commit
306ae13467
@ -9,10 +9,11 @@ use rocket::{
|
|||||||
form::{self, Form, ValueField},
|
form::{self, Form, ValueField},
|
||||||
fs::FileServer,
|
fs::FileServer,
|
||||||
http::{Cookie, CookieJar},
|
http::{Cookie, CookieJar},
|
||||||
response::Redirect,
|
request::FlashMessage,
|
||||||
|
response::{Flash, Redirect},
|
||||||
Build, Rocket, State,
|
Build, Rocket, State,
|
||||||
};
|
};
|
||||||
use rocket_dyn_templates::{context, Template};
|
use rocket_dyn_templates::{tera, Template};
|
||||||
use sea_orm::{Database, DatabaseConnection};
|
use sea_orm::{Database, DatabaseConnection};
|
||||||
use sha3::{Digest, Sha3_256};
|
use sha3::{Digest, Sha3_256};
|
||||||
|
|
||||||
@ -37,7 +38,12 @@ impl Deref for NaiveDateForm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/?<all>")]
|
#[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 data = Vec::new();
|
||||||
|
|
||||||
let mut show_next_n_days = 6;
|
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);
|
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")]
|
#[get("/name")]
|
||||||
fn name() -> Template {
|
fn name(flash: Option<FlashMessage<'_>>) -> Template {
|
||||||
Template::render("name", context! {})
|
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)]
|
#[derive(FromForm)]
|
||||||
@ -75,32 +93,31 @@ async fn savename(
|
|||||||
name: Form<NameForm>,
|
name: Form<NameForm>,
|
||||||
cookies: &CookieJar<'_>,
|
cookies: &CookieJar<'_>,
|
||||||
db: &State<DatabaseConnection>,
|
db: &State<DatabaseConnection>,
|
||||||
) -> Redirect {
|
) -> Flash<Redirect> {
|
||||||
let user = user::Model::find_or_create_user(&name.name, db.inner()).await;
|
let user = user::Model::find_or_create_user(&name.name, db.inner()).await;
|
||||||
match user.pw {
|
match user.pw {
|
||||||
Some(pw) => {
|
Some(pw) => match &name.pw {
|
||||||
match &name.pw {
|
Some(entered_pw) => {
|
||||||
Some(entered_pw) => {
|
let mut hasher = Sha3_256::new();
|
||||||
let mut hasher = Sha3_256::new();
|
hasher.update(entered_pw);
|
||||||
hasher.update(entered_pw);
|
let entered_pw = hasher.finalize();
|
||||||
let entered_pw = hasher.finalize();
|
|
||||||
|
|
||||||
if hex::encode(entered_pw) == pw {
|
if hex::encode(entered_pw) == pw {
|
||||||
cookies.add_private(Cookie::new("name", name.name.clone()));
|
cookies.add_private(Cookie::new("name", name.name.clone()));
|
||||||
} else {
|
return Flash::success(Redirect::to("/"), "Erfolgreich eingeloggt");
|
||||||
Redirect::to("/"); // Wrong PW
|
} else {
|
||||||
}
|
return Flash::error(Redirect::to("/name"), "Falsches Passwort");
|
||||||
}
|
|
||||||
None => {
|
|
||||||
Redirect::to("/"); // No PW supplied
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
None => {
|
||||||
|
return Flash::error(Redirect::to("/name"), "Benutzer besitzt hat Passwort, du hast jedoch keines eingegeben. Bitte nochmal probieren");
|
||||||
|
}
|
||||||
|
},
|
||||||
None => {
|
None => {
|
||||||
cookies.add_private(Cookie::new("name", name.name.clone()));
|
cookies.add_private(Cookie::new("name", name.name.clone()));
|
||||||
|
return Flash::success(Redirect::to("/"), "Name erfolgreich ausgewählt");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Redirect::to("/")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/logout")]
|
#[get("/logout")]
|
||||||
|
@ -43,6 +43,25 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
<a href="/logout">LOGOUT</a>
|
<a href="/logout">LOGOUT</a>
|
||||||
{% endif %}
|
{% 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="row">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user