This commit is contained in:
JMARyA 2025-01-30 14:30:58 +01:00
parent 5ce50b76f5
commit 66ef5dc543
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 18 additions and 3 deletions

View file

@ -1,7 +1,7 @@
use maud::{PreEscaped, html}; use maud::PreEscaped;
use super::User; use super::User;
use crate::get_pg; use crate::{get_pg, ui::prelude::script};
use std::str::FromStr; use std::str::FromStr;
pub trait CSRF { pub trait CSRF {
@ -15,7 +15,10 @@ impl CSRF for User {
/// ///
/// This is useful for htmx requests to update the CSRF token in place. /// This is useful for htmx requests to update the CSRF token in place.
async fn update_csrf(&self) -> PreEscaped<String> { async fn update_csrf(&self) -> PreEscaped<String> {
html! { script { (PreEscaped(format!("document.querySelectorAll('.csrf').forEach(element => {{ element.value = '{}'; }});", self.get_csrf().await))) }; } script(&format!(
"document.querySelectorAll('.csrf').forEach(element => {{ element.value = '{}'; }});",
self.get_csrf().await
))
} }
/// Get CSRF Token for the current session /// Get CSRF Token for the current session

View file

@ -1,5 +1,6 @@
#![feature(const_vec_string_slice)] #![feature(const_vec_string_slice)]
use rand::RngCore;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
pub mod asset; pub mod asset;
@ -13,6 +14,11 @@ pub mod ui;
// Postgres // Postgres
// TODO : Background Jobs
// TODO : Refactor caching
// TODO : mail
// TODO : scheduled jobs
// TODO : IDEA // TODO : IDEA
// more efficient table join using WHERE ANY instead of multiple SELECTs // more efficient table join using WHERE ANY instead of multiple SELECTs
// map_tables(Vec<T>, Fn(&T) -> U) -> Vec<U> // map_tables(Vec<T>, Fn(&T) -> U) -> Vec<U>
@ -46,3 +52,9 @@ macro_rules! get_pg {
} }
}; };
} }
pub fn gen_random(len: usize) -> String {
let mut data = vec![0u8; len];
rand::thread_rng().fill_bytes(&mut data);
data_encoding::HEXLOWER.encode(&data)
}