Permet de fermer les inscriptions via REGISTRATION_ENABLED
- Chargement du fichier .env au démarrage (dotenvy). - REGISTRATION_ENABLED=false refuse l'inscription côté serveur, masque le lien « Créer un compte » et remplace le formulaire par un message. - Ajout de .env.example ; .env ignoré par git. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cbda09ed40
commit
cbaab7234b
8 files changed
+55
-2
No files matched your search
@@ -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
|
||||
@@ -1,2 +1,3 @@
|
||||
/target
|
||||
lisette.db*
|
||||
.env
|
||||
Generated
+1
@@ -1395,6 +1395,7 @@ dependencies = [
|
||||
"argon2",
|
||||
"axum",
|
||||
"console_error_panic_hook",
|
||||
"dotenvy",
|
||||
"hex",
|
||||
"leptos",
|
||||
"leptos_axum",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -92,9 +92,17 @@ pub async fn get_me() -> Result<Option<User>, ServerFnError> {
|
||||
crate::auth::current_user(&pool()?).await.map_err(db_err)
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn registration_enabled() -> Result<bool, ServerFnError> {
|
||||
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)?;
|
||||
|
||||
+29
-2
@@ -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<Login>,
|
||||
pub register: ServerAction<Register>,
|
||||
pub logout: ServerAction<Logout>,
|
||||
/// Whether sign-ups are open (`REGISTRATION_ENABLED`).
|
||||
pub registration: Resource<Result<bool, ServerFnError>>,
|
||||
}
|
||||
|
||||
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 {
|
||||
</Field>
|
||||
<SubmitButton pending=auth.login.pending() label="Se connecter" />
|
||||
</ActionForm>
|
||||
<Suspense>
|
||||
{move || Suspend::new(async move {
|
||||
matches!(auth.registration.await, Ok(true))
|
||||
.then(|| {
|
||||
view! {
|
||||
<p class="mt-6 text-center text-ink/70">
|
||||
"Pas encore de compte ? "
|
||||
<A href="/inscription" attr:class="font-semibold text-ink underline">
|
||||
"Créer un compte"
|
||||
</A>
|
||||
</p>
|
||||
}
|
||||
})
|
||||
})}
|
||||
</Suspense>
|
||||
</AuthLayout>
|
||||
}
|
||||
}
|
||||
@@ -130,6 +142,17 @@ pub fn RegisterPage() -> impl IntoView {
|
||||
let auth = expect_context::<Auth>();
|
||||
view! {
|
||||
<AuthLayout title="Créer un compte">
|
||||
<Suspense>
|
||||
{move || Suspend::new(async move {
|
||||
if !matches!(auth.registration.await, Ok(true)) {
|
||||
return view! {
|
||||
<p class="mt-4 text-lg text-ink/70">
|
||||
"Les inscriptions sont fermées pour le moment."
|
||||
</p>
|
||||
}
|
||||
.into_any();
|
||||
}
|
||||
view! {
|
||||
<ActionForm action=auth.register attr:class="mt-5 flex flex-col gap-4">
|
||||
<FormError error=Signal::derive(move || auth.register.value().get().and_then(Result::err).map(error_message)) />
|
||||
<Field label="Prénom ou pseudo">
|
||||
@@ -157,6 +180,10 @@ pub fn RegisterPage() -> impl IntoView {
|
||||
</Field>
|
||||
<SubmitButton pending=auth.register.pending() label="Créer mon compte" />
|
||||
</ActionForm>
|
||||
}
|
||||
.into_any()
|
||||
})}
|
||||
</Suspense>
|
||||
<p class="mt-6 text-center text-ink/70">
|
||||
"Déjà inscrit·e ? "
|
||||
<A href="/connexion" attr:class="font-semibold text-ink underline">
|
||||
|
||||
@@ -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<String, argon2::password_hash::Error> {
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
Ok(Argon2::default().hash_password(password.as_bytes(), &salt)?.to_string())
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user