clean code; if logged out: save url where user tried to go to, go there once logged in, Fixes #179
Some checks failed
CI/CD Pipeline / deploy-staging (push) Blocked by required conditions
CI/CD Pipeline / deploy-main (push) Blocked by required conditions
CI/CD Pipeline / test (push) Has been cancelled

This commit is contained in:
2024-01-22 22:08:05 +01:00
parent 60b9a4dbba
commit b7499bd6cb
8 changed files with 41 additions and 34 deletions

View File

@ -1,5 +1,4 @@
use rocket::form::Form;
use rocket::fs::TempFile;
use rocket::response::{Flash, Redirect};
use rocket::{get, request::FlashMessage, routes, Route, State};
use rocket::{post, FromForm};
@ -34,28 +33,24 @@ async fn index(
}
#[get("/mail/fee")]
async fn fee(
db: &State<SqlitePool>,
admin: AdminUser,
config: &State<Config>,
flash: Option<FlashMessage<'_>>,
) -> &'static str {
async fn fee(db: &State<SqlitePool>, _admin: AdminUser, config: &State<Config>) -> &'static str {
Mail::fees(db, config.smtp_pw.clone()).await;
"SUCC"
}
#[derive(FromForm, Debug)]
pub struct MailToSend<'a> {
pub struct MailToSend {
//<'a> {
pub(crate) role_id: i32,
pub(crate) subject: String,
pub(crate) body: String,
pub(crate) files: Vec<TempFile<'a>>,
//pub(crate) files: Vec<TempFile<'a>>,
}
#[post("/mail", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<MailToSend<'_>>,
data: Form<MailToSend>,
config: &State<Config>,
_admin: AdminUser,
) -> Flash<Redirect> {

View File

@ -3,9 +3,9 @@ use std::collections::HashMap;
use crate::model::{
family::Family,
role::Role,
user::{AdminUser, Fee, User, UserWithRoles, VorstandUser},
user::{AdminUser, User, UserWithRoles, VorstandUser},
};
use futures::future::{self, join_all};
use futures::future::join_all;
use rocket::{
form::Form,
get, post,
@ -80,7 +80,7 @@ async fn fees(
#[get("/user/fees/paid?<user_ids>")]
async fn fees_paid(
db: &State<SqlitePool>,
admin: AdminUser,
_admin: AdminUser,
user_ids: Vec<i32>,
) -> Flash<Redirect> {
let mut res = String::new();

View File

@ -89,7 +89,15 @@ async fn login(
)
.await;
Flash::success(Redirect::to("/"), "Login erfolgreich")
// Check for redirect_url cookie and redirect accordingly
match cookies.get_private("redirect_url") {
Some(redirect_cookie) => {
let redirect_url = redirect_cookie.value().to_string();
cookies.remove_private(redirect_cookie); // Remove the cookie after using it
Flash::success(Redirect::to(redirect_url), "Login erfolgreich")
}
None => Flash::success(Redirect::to("/"), "Login erfolgreich"),
}
}
#[get("/set-pw/<userid>")]

View File

@ -125,7 +125,7 @@ async fn new_kiosk(
async fn kiosk(
db: &State<SqlitePool>,
flash: Option<FlashMessage<'_>>,
kiosk: KioskCookie,
_kiosk: KioskCookie,
) -> Template {
let boats = Boat::all(db).await;
let coxes: Vec<UserWithWaterStatus> = futures::future::join_all(

View File

@ -3,10 +3,14 @@ use rocket::{
fairing::AdHoc,
form::Form,
fs::FileServer,
get, post,
get,
http::Cookie,
post,
request::FlashMessage,
response::{Flash, Redirect},
routes, Build, FromForm, Rocket, State,
routes,
time::{Duration, OffsetDateTime},
Build, FromForm, Request, Rocket, State,
};
use rocket_dyn_templates::Template;
use serde::Deserialize;
@ -51,7 +55,13 @@ async fn wikiauth(db: &State<SqlitePool>, login: Form<LoginForm<'_>>) -> String
}
#[catch(401)] //Unauthorized
fn unauthorized_error() -> Redirect {
fn unauthorized_error(req: &Request) -> Redirect {
// Save the URL the user tried to access, to be able to go there once logged in
let mut redirect_cookie = Cookie::new("redirect_url", format!("{}", req.uri()));
println!("{}", req.uri());
redirect_cookie.set_expires(OffsetDateTime::now_utc() + Duration::hours(1));
req.cookies().add_private(redirect_cookie);
Redirect::to("/auth")
}