diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7f4cdb4 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +# Base SQLite (créée si absente) +DATABASE_URL=sqlite://lisette.db + +# Mettre à false pour fermer les inscriptions (ouvertes par défaut) +REGISTRATION_ENABLED=true diff --git a/.gitignore b/.gitignore index 3b61748..af7c854 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target lisette.db* +.env diff --git a/Cargo.lock b/Cargo.lock index 21a5202..159cb83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1395,6 +1395,7 @@ dependencies = [ "argon2", "axum", "console_error_panic_hook", + "dotenvy", "hex", "leptos", "leptos_axum", diff --git a/Cargo.toml b/Cargo.toml index cc2979e..64cc093 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ argon2 = { version = "0.5", optional = true } rand_core = { version = "0.6", features = ["getrandom"], optional = true } sha2 = { version = "0.10", optional = true } hex = { version = "0.4", optional = true } +dotenvy = { version = "0.15", optional = true } serde = { version = "1", features = ["derive"] } wasm-bindgen = { version = "=0.2.128", optional = true } console_error_panic_hook = { version = "0.1", optional = true } @@ -33,6 +34,7 @@ ssr = [ "dep:rand_core", "dep:sha2", "dep:hex", + "dep:dotenvy", "leptos/ssr", "leptos_meta/ssr", "leptos_router/ssr", diff --git a/src/api.rs b/src/api.rs index 1bee68f..1d00650 100644 --- a/src/api.rs +++ b/src/api.rs @@ -92,9 +92,17 @@ pub async fn get_me() -> Result, ServerFnError> { crate::auth::current_user(&pool()?).await.map_err(db_err) } +#[server] +pub async fn registration_enabled() -> Result { + Ok(crate::auth::registration_enabled()) +} + #[server] pub async fn register(name: String, email: String, password: String) -> Result<(), ServerFnError> { use server::*; + if !crate::auth::registration_enabled() { + return Err(ServerFnError::new("Les inscriptions sont fermées.")); + } let pool = pool()?; let name = clean_name(&name, 40)?; let email = clean_email(&email)?; diff --git a/src/app/account.rs b/src/app/account.rs index 4815a38..ccafd40 100644 --- a/src/app/account.rs +++ b/src/app/account.rs @@ -3,7 +3,7 @@ use leptos_router::components::{A, Redirect}; use super::sheets::Sheet; use super::{Ctx, error_message}; -use crate::api::{Login, Logout, Register, get_me}; +use crate::api::{Login, Logout, Register, get_me, registration_enabled}; use crate::model::User; /// Session state shared by the whole app. @@ -13,6 +13,8 @@ pub struct Auth { pub login: ServerAction, pub register: ServerAction, pub logout: ServerAction, + /// Whether sign-ups are open (`REGISTRATION_ENABLED`). + pub registration: Resource>, } impl Auth { @@ -24,7 +26,8 @@ impl Auth { move || (login.version().get(), register.version().get(), logout.version().get()), |_| get_me(), ); - let auth = Auth { me, login, register, logout }; + let registration = Resource::new(|| (), |_| registration_enabled()); + let auth = Auth { me, login, register, logout, registration }; provide_context(auth); auth } @@ -115,12 +118,21 @@ pub fn LoginPage() -> impl IntoView { -

- "Pas encore de compte ? " - - "Créer un compte" - -

+ + {move || Suspend::new(async move { + matches!(auth.registration.await, Ok(true)) + .then(|| { + view! { +

+ "Pas encore de compte ? " + + "Créer un compte" + +

+ } + }) + })} +
} } @@ -130,33 +142,48 @@ pub fn RegisterPage() -> impl IntoView { let auth = expect_context::(); view! { - - - - - - - - - - - - - + + {move || Suspend::new(async move { + if !matches!(auth.registration.await, Ok(true)) { + return view! { +

+ "Les inscriptions sont fermées pour le moment." +

+ } + .into_any(); + } + view! { + + + + + + + + + + + + + + } + .into_any() + })} +

"Déjà inscrit·e ? " diff --git a/src/auth.rs b/src/auth.rs index 4b54ac4..6bd677f 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -16,6 +16,13 @@ use crate::model::User; const COOKIE: &str = "lisette_session"; const SESSION_DAYS: i64 = 30; +/// `REGISTRATION_ENABLED=false` (env or `.env`) closes sign-ups; open by default. +pub fn registration_enabled() -> bool { + std::env::var("REGISTRATION_ENABLED") + .map(|value| !matches!(value.trim().to_lowercase().as_str(), "false" | "0" | "no" | "off")) + .unwrap_or(true) +} + pub fn hash_password(password: &str) -> Result { let salt = SaltString::generate(&mut OsRng); Ok(Argon2::default().hash_password(password.as_bytes(), &salt)?.to_string()) diff --git a/src/main.rs b/src/main.rs index f72d093..6a20ec4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ async fn main() { use leptos_axum::{LeptosRoutes, generate_route_list}; use lisette::app::{App, shell}; + dotenvy::dotenv().ok(); + let conf = get_configuration(None).expect("invalid Leptos configuration"); let leptos_options = conf.leptos_options; let addr = leptos_options.site_addr;