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.
This commit is contained in:
parent
e08a46af3a
commit
a0b79a17ea
6 changed files with 22 additions and 22 deletions
|
@ -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] Being able to change from one week to the next
|
||||||
- [x] Create new weeks for the admin
|
- [x] Create new weeks for the admin
|
||||||
- [ ] Proper week redirection
|
- [x] Proper week redirection
|
||||||
- [ ] Correctly handle non-existing week number
|
- [ ] Correctly handle non-existing week number
|
||||||
- [x] Add introduction to the weekly truths
|
- [x] Add introduction to the weekly truths
|
||||||
- [ ] Bundle static assets in the binary
|
- [ ] Bundle static assets in the binary
|
||||||
|
|
16
src/auth.rs
16
src/auth.rs
|
@ -9,7 +9,7 @@ use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
||||||
use blake2::{Blake2b512, Digest};
|
use blake2::{Blake2b512, Digest};
|
||||||
use blake2::digest::FixedOutput;
|
use blake2::digest::FixedOutput;
|
||||||
use crate::database_records::{AuthTokens, PlayerLoginInfo, Vote};
|
use crate::database_records::{AuthTokens, PlayerLoginInfo, Vote};
|
||||||
use crate::database;
|
use crate::{database, week};
|
||||||
use database::Db;
|
use database::Db;
|
||||||
|
|
||||||
// TODO: Make FromRequest guard https://api.rocket.rs/v0.5/rocket/request/trait.FromRequest and split admin
|
// 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
|
password: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/login", data="<form>")]
|
#[post("/<week>/login", data="<form>")]
|
||||||
pub async fn login(form: Form<AuthForm>, mut db: Connection<Db>, cookies: &CookieJar<'_>) -> Redirect {
|
pub async fn login(week: u8, form: Form<AuthForm>, mut db: Connection<Db>, cookies: &CookieJar<'_>) -> Redirect {
|
||||||
let user_search: Result<PlayerLoginInfo, _> = sqlx::query_as("SELECT id, is_admin, name, pwd_hash FROM Players WHERE name == $1")
|
let user_search: Result<PlayerLoginInfo, _> = sqlx::query_as("SELECT id, is_admin, name, pwd_hash FROM Players WHERE name == $1")
|
||||||
.bind(&form.name)
|
.bind(&form.name)
|
||||||
.fetch_one(&mut **db)
|
.fetch_one(&mut **db)
|
||||||
|
@ -136,7 +136,7 @@ pub async fn login(form: Form<AuthForm>, mut db: Connection<Db>, cookies: &Cooki
|
||||||
if user_search.is_err() {
|
if user_search.is_err() {
|
||||||
error!("Login failed : invalid user {:?}, err: {:?}", form.name, user_search.err());
|
error!("Login failed : invalid user {:?}, err: {:?}", form.name, user_search.err());
|
||||||
cookies.add(("toast_error", "Impossible de se connecter !"));
|
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();
|
let new_user = user_search.unwrap();
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ pub async fn login(form: Form<AuthForm>, mut db: Connection<Db>, cookies: &Cooki
|
||||||
if password_hash_parse.is_err() {
|
if password_hash_parse.is_err() {
|
||||||
error!("Login failed : could not parse password hash {:?}", password_hash_parse.err());
|
error!("Login failed : could not parse password hash {:?}", password_hash_parse.err());
|
||||||
cookies.add(("toast_error", "Impossible de se connecter !"));
|
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();
|
let password_hash = password_hash_parse.unwrap();
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ pub async fn login(form: Form<AuthForm>, mut db: Connection<Db>, cookies: &Cooki
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
error!("Login failed : coult not store auth token in database : {error}");
|
error!("Login failed : coult not store auth token in database : {error}");
|
||||||
cookies.add(("toast_error", "Impossible de se connecter !"));
|
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<AuthForm>, mut db: Connection<Db>, cookies: &Cooki
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Login failed : invalid password for {:?}\nError : {err}", new_user.name);
|
error!("Login failed : invalid password for {:?}\nError : {err}", new_user.name);
|
||||||
cookies.add(("toast_error", "Impossible de se connecter !"));
|
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<'_>) {
|
pub fn bypass_auth_debug(cookies: &CookieJar<'_>) {
|
||||||
|
|
12
src/truth.rs
12
src/truth.rs
|
@ -6,7 +6,7 @@ use rocket_db_pools::{sqlx, Connection};
|
||||||
|
|
||||||
use pulldown_cmark::{Parser, Options};
|
use pulldown_cmark::{Parser, Options};
|
||||||
use sqlx::Row;
|
use sqlx::Row;
|
||||||
use crate::{auth, database};
|
use crate::{auth, database, week};
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
pub struct TruthUpdateForm {
|
pub struct TruthUpdateForm {
|
||||||
|
@ -20,7 +20,7 @@ pub async fn edit_truth(week: u8, truth_number: u8, form: Form<TruthUpdateForm>,
|
||||||
let user = auth::get_user(week, &mut db, cookies).await;
|
let user = auth::get_user(week, &mut db, cookies).await;
|
||||||
if !user.is_admin {
|
if !user.is_admin {
|
||||||
cookies.add(("toast_error", "Vous n'avez pas la permission de changer la vérité."));
|
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();
|
let mut options = Options::empty();
|
||||||
|
@ -50,7 +50,7 @@ pub async fn edit_truth(week: u8, truth_number: u8, form: Form<TruthUpdateForm>,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Redirect::to(uri!("/"))
|
Redirect::to(uri!(week::week(week)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/<week>/new_truth", data="<form>")]
|
#[post("/<week>/new_truth", data="<form>")]
|
||||||
|
@ -59,7 +59,7 @@ pub async fn create_truth(week: u8, form: Form<TruthUpdateForm>,
|
||||||
let user = auth::get_user(week, &mut db, cookies).await;
|
let user = auth::get_user(week, &mut db, cookies).await;
|
||||||
if !user.is_admin {
|
if !user.is_admin {
|
||||||
cookies.add(("toast_error", "Vous n'avez pas la permission d'ajouter de vérité."));
|
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;")
|
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<TruthUpdateForm>,
|
||||||
if truth_number == 0 {
|
if truth_number == 0 {
|
||||||
error!("Error while getting max truth number.");
|
error!("Error while getting max truth number.");
|
||||||
cookies.add(("toast_error", "Erreur lors de l'ajout de la vérité..."));
|
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();
|
let mut options = Options::empty();
|
||||||
|
@ -108,5 +108,5 @@ pub async fn create_truth(week: u8, form: Form<TruthUpdateForm>,
|
||||||
|
|
||||||
debug!("Truth was successfully added");
|
debug!("Truth was successfully added");
|
||||||
|
|
||||||
Redirect::to(uri!("/"))
|
Redirect::to(uri!(week::week(week)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ use rocket::serde::json::Json;
|
||||||
|
|
||||||
use rocket_db_pools::{sqlx, Connection};
|
use rocket_db_pools::{sqlx, Connection};
|
||||||
|
|
||||||
use crate::{auth, database};
|
use crate::{auth, database, week};
|
||||||
use crate::database_records::{Vote, VotingData};
|
use crate::database_records::{Vote, VotingData};
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
|
@ -25,7 +25,7 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
|
||||||
|
|
||||||
if !user.logged_in {
|
if !user.logged_in {
|
||||||
cookies.add(("toast_error", "Vous n'avez pas la permission de changer de vote."));
|
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(
|
let filtered_votes = form.truth_votes.iter().filter_map(
|
||||||
|
@ -85,7 +85,7 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
|
||||||
debug!("Vote successful")
|
debug!("Vote successful")
|
||||||
}
|
}
|
||||||
|
|
||||||
Redirect::to(uri!("/"))
|
Redirect::to(uri!(week::week(week)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub async fn week(week_number: u8, mut db: Connection<Db>, cookies: &CookieJar<'
|
||||||
.fetch_all(&mut **db).await {
|
.fetch_all(&mut **db).await {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
println!("Some error while getting players : {error}");
|
error!("Some error while getting players : {error}");
|
||||||
Vec::<Player>::new()
|
Vec::<Player>::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ pub async fn update_week(week: u8, raw_intro: Form<String>,
|
||||||
let user = auth::get_user(week, &mut db, cookies).await;
|
let user = auth::get_user(week, &mut db, cookies).await;
|
||||||
if !user.is_admin {
|
if !user.is_admin {
|
||||||
cookies.add(("toast_error", "Vous n'avez pas la permission de changer la semaine."));
|
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();
|
let mut options = Options::empty();
|
||||||
|
@ -113,7 +113,7 @@ pub async fn update_week(week: u8, raw_intro: Form<String>,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Redirect::to(uri!("/"))
|
Redirect::to(uri!(week(week)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/<week>/set_last")]
|
#[post("/<week>/set_last")]
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
{% if user.logged_in == true %}
|
{% if user.logged_in == true %}
|
||||||
<p>Connecté en tant que <b>{{ user.name }}</b></p>
|
<p>Connecté en tant que <b>{{ user.name }}</b></p>
|
||||||
{% else %}
|
{% else %}
|
||||||
<form class="login" id="login" action="/login" method="POST">
|
<form class="login" id="login" action="/{{ week_data.number }}/login" method="POST">
|
||||||
<label>Pseudo <input form="login" type="text" name="name"/></label>
|
<label>Pseudo <input form="login" type="text" name="name"/></label>
|
||||||
<label>Mot de passe <input form="login" type="password" name="password"/></label>
|
<label>Mot de passe <input form="login" type="password" name="password"/></label>
|
||||||
<button form="login">Se connecter</button>
|
<button form="login">Se connecter</button>
|
||||||
|
|
Loading…
Add table
Reference in a new issue