From a0b79a17ea0a583c6773db43b9dbc1b29b334aa9 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Fri, 26 Jul 2024 01:08:30 +0100 Subject: [PATCH] Redirects: properly redirect to the current week Previously, most redirects targeted the root of the application. This was okay for the first part of development where only one week would be active, but would be annoying when using multiple weeks. Change those redirects to call week::week. Change the login path to be dependant on the current week as well, so it can be correctly redirected. --- README.md | 2 +- src/auth.rs | 16 ++++++++-------- src/truth.rs | 12 ++++++------ src/vote.rs | 6 +++--- src/week.rs | 6 +++--- templates/index.html.tera | 2 +- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ac48cb6..2b6d940 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A list of things that could be implemented/added to the application, some of the - [x] Being able to change from one week to the next - [x] Create new weeks for the admin - - [ ] Proper week redirection + - [x] Proper week redirection - [ ] Correctly handle non-existing week number - [x] Add introduction to the weekly truths - [ ] Bundle static assets in the binary diff --git a/src/auth.rs b/src/auth.rs index 1fd62fa..7f6b251 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -9,7 +9,7 @@ use argon2::{Argon2, PasswordHash, PasswordVerifier}; use blake2::{Blake2b512, Digest}; use blake2::digest::FixedOutput; use crate::database_records::{AuthTokens, PlayerLoginInfo, Vote}; -use crate::database; +use crate::{database, week}; use database::Db; // TODO: Make FromRequest guard https://api.rocket.rs/v0.5/rocket/request/trait.FromRequest and split admin @@ -126,8 +126,8 @@ pub struct AuthForm { password: String } -#[post("/login", data="
")] -pub async fn login(form: Form, mut db: Connection, cookies: &CookieJar<'_>) -> Redirect { +#[post("//login", data="")] +pub async fn login(week: u8, form: Form, mut db: Connection, cookies: &CookieJar<'_>) -> Redirect { let user_search: Result = sqlx::query_as("SELECT id, is_admin, name, pwd_hash FROM Players WHERE name == $1") .bind(&form.name) .fetch_one(&mut **db) @@ -136,7 +136,7 @@ pub async fn login(form: Form, mut db: Connection, cookies: &Cooki if user_search.is_err() { error!("Login failed : invalid user {:?}, err: {:?}", form.name, user_search.err()); cookies.add(("toast_error", "Impossible de se connecter !")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } let new_user = user_search.unwrap(); @@ -144,7 +144,7 @@ pub async fn login(form: Form, mut db: Connection, cookies: &Cooki if password_hash_parse.is_err() { error!("Login failed : could not parse password hash {:?}", password_hash_parse.err()); cookies.add(("toast_error", "Impossible de se connecter !")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } let password_hash = password_hash_parse.unwrap(); @@ -168,7 +168,7 @@ pub async fn login(form: Form, mut db: Connection, cookies: &Cooki Err(error) => { error!("Login failed : coult not store auth token in database : {error}"); cookies.add(("toast_error", "Impossible de se connecter !")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } } @@ -178,11 +178,11 @@ pub async fn login(form: Form, mut db: Connection, cookies: &Cooki Err(err) => { error!("Login failed : invalid password for {:?}\nError : {err}", new_user.name); cookies.add(("toast_error", "Impossible de se connecter !")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } } - Redirect::to(uri!("/")) + Redirect::to(uri!(week::week(week))) } pub fn bypass_auth_debug(cookies: &CookieJar<'_>) { diff --git a/src/truth.rs b/src/truth.rs index ae96101..861449c 100644 --- a/src/truth.rs +++ b/src/truth.rs @@ -6,7 +6,7 @@ use rocket_db_pools::{sqlx, Connection}; use pulldown_cmark::{Parser, Options}; use sqlx::Row; -use crate::{auth, database}; +use crate::{auth, database, week}; #[derive(FromForm)] pub struct TruthUpdateForm { @@ -20,7 +20,7 @@ pub async fn edit_truth(week: u8, truth_number: u8, form: Form, let user = auth::get_user(week, &mut db, cookies).await; if !user.is_admin { cookies.add(("toast_error", "Vous n'avez pas la permission de changer la vérité.")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } let mut options = Options::empty(); @@ -50,7 +50,7 @@ pub async fn edit_truth(week: u8, truth_number: u8, form: Form, } }; - Redirect::to(uri!("/")) + Redirect::to(uri!(week::week(week))) } #[post("//new_truth", data="")] @@ -59,7 +59,7 @@ pub async fn create_truth(week: u8, form: Form, let user = auth::get_user(week, &mut db, cookies).await; if !user.is_admin { cookies.add(("toast_error", "Vous n'avez pas la permission d'ajouter de vérité.")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } let truth_number: u8 = match sqlx::query("SELECT max(number) from Truths WHERE week == $1;") @@ -76,7 +76,7 @@ pub async fn create_truth(week: u8, form: Form, if truth_number == 0 { error!("Error while getting max truth number."); cookies.add(("toast_error", "Erreur lors de l'ajout de la vérité...")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } let mut options = Options::empty(); @@ -108,5 +108,5 @@ pub async fn create_truth(week: u8, form: Form, debug!("Truth was successfully added"); - Redirect::to(uri!("/")) + Redirect::to(uri!(week::week(week))) } diff --git a/src/vote.rs b/src/vote.rs index 831068c..f3becde 100644 --- a/src/vote.rs +++ b/src/vote.rs @@ -10,7 +10,7 @@ use rocket::serde::json::Json; use rocket_db_pools::{sqlx, Connection}; -use crate::{auth, database}; +use crate::{auth, database, week}; use crate::database_records::{Vote, VotingData}; #[derive(FromForm)] @@ -25,7 +25,7 @@ pub async fn vote(week: u8, form: Form, if !user.logged_in { cookies.add(("toast_error", "Vous n'avez pas la permission de changer de vote.")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week::week(week))); } let filtered_votes = form.truth_votes.iter().filter_map( @@ -85,7 +85,7 @@ pub async fn vote(week: u8, form: Form, debug!("Vote successful") } - Redirect::to(uri!("/")) + Redirect::to(uri!(week::week(week))) } #[derive(Serialize, Deserialize)] diff --git a/src/week.rs b/src/week.rs index 6e6de1a..1eb52db 100644 --- a/src/week.rs +++ b/src/week.rs @@ -22,7 +22,7 @@ pub async fn week(week_number: u8, mut db: Connection, cookies: &CookieJar<' .fetch_all(&mut **db).await { Ok(v) => v, Err(error) => { - println!("Some error while getting players : {error}"); + error!("Some error while getting players : {error}"); Vec::::new() } } @@ -85,7 +85,7 @@ pub async fn update_week(week: u8, raw_intro: Form, let user = auth::get_user(week, &mut db, cookies).await; if !user.is_admin { cookies.add(("toast_error", "Vous n'avez pas la permission de changer la semaine.")); - return Redirect::to(uri!("/")); + return Redirect::to(uri!(week(week))); } let mut options = Options::empty(); @@ -113,7 +113,7 @@ pub async fn update_week(week: u8, raw_intro: Form, } }; - Redirect::to(uri!("/")) + Redirect::to(uri!(week(week))) } #[post("//set_last")] diff --git a/templates/index.html.tera b/templates/index.html.tera index cd11ae4..c2f3afa 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -42,7 +42,7 @@ {% if user.logged_in == true %}

Connecté en tant que {{ user.name }}

{% else %} - +