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
+88
-35
No files matched your search
@@ -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)?;
|
||||
|
||||
+62
-35
@@ -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>
|
||||
<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>
|
||||
{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,33 +142,48 @@ pub fn RegisterPage() -> impl IntoView {
|
||||
let auth = expect_context::<Auth>();
|
||||
view! {
|
||||
<AuthLayout title="Créer un compte">
|
||||
<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">
|
||||
<input
|
||||
class="field"
|
||||
name="name"
|
||||
required
|
||||
maxlength="40"
|
||||
autocomplete="given-name"
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Email">
|
||||
<input class="field" type="email" name="email" required autocomplete="email" />
|
||||
</Field>
|
||||
<Field label="Mot de passe (8 caractères min.)">
|
||||
<input
|
||||
class="field"
|
||||
type="password"
|
||||
name="password"
|
||||
required
|
||||
minlength="8"
|
||||
maxlength="128"
|
||||
autocomplete="new-password"
|
||||
/>
|
||||
</Field>
|
||||
<SubmitButton pending=auth.register.pending() label="Créer mon compte" />
|
||||
</ActionForm>
|
||||
<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">
|
||||
<input
|
||||
class="field"
|
||||
name="name"
|
||||
required
|
||||
maxlength="40"
|
||||
autocomplete="given-name"
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Email">
|
||||
<input class="field" type="email" name="email" required autocomplete="email" />
|
||||
</Field>
|
||||
<Field label="Mot de passe (8 caractères min.)">
|
||||
<input
|
||||
class="field"
|
||||
type="password"
|
||||
name="password"
|
||||
required
|
||||
minlength="8"
|
||||
maxlength="128"
|
||||
autocomplete="new-password"
|
||||
/>
|
||||
</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