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

@ -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")
}