- Inscription / connexion par email + mot de passe (argon2), prénom ou pseudo enregistré ; session par cookie HttpOnly, jeton haché en base. - Listes privées par propriétaire, partageables par email ; les membres peuvent gérer les articles et quitter la liste. - Contrôle d'accès dans chaque server function. - Suggestions propres à chaque utilisateur. - Avatar avec initiales et feuille de compte (déconnexion). - La page 404 reçoit le pool SQLite. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
217 lines
7.5 KiB
Rust
217 lines
7.5 KiB
Rust
use leptos::prelude::*;
|
|
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::model::User;
|
|
|
|
/// Session state shared by the whole app.
|
|
#[derive(Clone, Copy)]
|
|
pub struct Auth {
|
|
pub me: Resource<Result<Option<User>, ServerFnError>>,
|
|
pub login: ServerAction<Login>,
|
|
pub register: ServerAction<Register>,
|
|
pub logout: ServerAction<Logout>,
|
|
}
|
|
|
|
impl Auth {
|
|
pub fn provide() -> Self {
|
|
let login = ServerAction::<Login>::new();
|
|
let register = ServerAction::<Register>::new();
|
|
let logout = ServerAction::<Logout>::new();
|
|
let me = Resource::new(
|
|
move || (login.version().get(), register.version().get(), logout.version().get()),
|
|
|_| get_me(),
|
|
);
|
|
let auth = Auth { me, login, register, logout };
|
|
provide_context(auth);
|
|
auth
|
|
}
|
|
}
|
|
|
|
/// Renders `render` with the signed-in user, or redirects to the login page.
|
|
#[component]
|
|
pub fn RequireUser<F, V>(render: F) -> impl IntoView
|
|
where
|
|
F: Fn(User) -> V + Send + Sync + 'static,
|
|
V: IntoView + 'static,
|
|
{
|
|
let auth = expect_context::<Auth>();
|
|
let render = StoredValue::new(render);
|
|
view! {
|
|
<Transition>
|
|
{move || Suspend::new(async move {
|
|
match auth.me.await {
|
|
Ok(Some(user)) => render.with_value(|render| render(user)).into_any(),
|
|
_ => view! { <Redirect path="/connexion" /> }.into_any(),
|
|
}
|
|
})}
|
|
</Transition>
|
|
}
|
|
}
|
|
|
|
/// Sends a signed-in user away from the login and sign-up pages.
|
|
#[component]
|
|
fn RedirectIfSignedIn() -> impl IntoView {
|
|
let auth = expect_context::<Auth>();
|
|
view! {
|
|
<Transition>
|
|
{move || Suspend::new(async move {
|
|
matches!(auth.me.await, Ok(Some(_))).then(|| view! { <Redirect path="/" /> })
|
|
})}
|
|
</Transition>
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn AuthLayout(title: &'static str, children: Children) -> impl IntoView {
|
|
view! {
|
|
<RedirectIfSignedIn />
|
|
<main class="mx-auto flex min-h-dvh max-w-md flex-col px-4 pt-[max(1.5rem,env(safe-area-inset-top))] pb-16">
|
|
<h1 class="font-display text-5xl leading-[0.9] font-extrabold tracking-tight">
|
|
"mes"<br />"listes"
|
|
</h1>
|
|
<section class="ticket relative mt-8 bg-paper px-5 pt-6 pb-8">
|
|
<p class="font-mono text-sm tracking-[0.2em] text-ink/60 uppercase">"Lisette · Courses"</p>
|
|
<h2 class="mt-1 font-display text-[1.7rem] leading-tight font-bold tracking-tight">{title}</h2>
|
|
{children()}
|
|
</section>
|
|
</main>
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn FormError(#[prop(into)] error: Signal<Option<String>>) -> impl IntoView {
|
|
move || {
|
|
error.get().map(|msg| {
|
|
view! {
|
|
<p role="alert" class="rounded-xl border-2 border-ink bg-pink px-3 py-2 font-medium">
|
|
{msg}
|
|
</p>
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
pub fn LoginPage() -> impl IntoView {
|
|
let auth = expect_context::<Auth>();
|
|
view! {
|
|
<AuthLayout title="Connexion">
|
|
<ActionForm action=auth.login attr:class="mt-5 flex flex-col gap-4">
|
|
<FormError error=Signal::derive(move || auth.login.value().get().and_then(Result::err).map(error_message)) />
|
|
<Field label="Email">
|
|
<input class="field" type="email" name="email" required autocomplete="email" />
|
|
</Field>
|
|
<Field label="Mot de passe">
|
|
<input
|
|
class="field"
|
|
type="password"
|
|
name="password"
|
|
required
|
|
autocomplete="current-password"
|
|
/>
|
|
</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>
|
|
</AuthLayout>
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
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>
|
|
<p class="mt-6 text-center text-ink/70">
|
|
"Déjà inscrit·e ? "
|
|
<A href="/connexion" attr:class="font-semibold text-ink underline">
|
|
"Se connecter"
|
|
</A>
|
|
</p>
|
|
</AuthLayout>
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn Field(label: &'static str, children: Children) -> impl IntoView {
|
|
view! {
|
|
<label class="flex flex-col gap-1.5">
|
|
<span class="font-mono text-xs tracking-[0.2em] text-ink/60 uppercase">{label}</span>
|
|
{children()}
|
|
</label>
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn SubmitButton(#[prop(into)] pending: Signal<bool>, label: &'static str) -> impl IntoView {
|
|
view! {
|
|
<button
|
|
type="submit"
|
|
class="tile-shadow mt-2 rounded-2xl border-2 border-ink bg-pink px-4 py-3 font-display text-lg font-bold active:translate-y-1 active:shadow-none disabled:opacity-60"
|
|
disabled=move || pending.get()
|
|
>
|
|
{label}
|
|
</button>
|
|
}
|
|
}
|
|
|
|
/// Avatar sheet: who is signed in, and sign out.
|
|
#[component]
|
|
pub fn AccountSheet(user: User) -> impl IntoView {
|
|
let auth = expect_context::<Auth>();
|
|
let ctx = expect_context::<Ctx>();
|
|
let close = move || ctx.account_open.set(false);
|
|
let user = StoredValue::new(user);
|
|
|
|
view! {
|
|
<Show when=move || ctx.account_open.get()>
|
|
<Sheet title=user.with_value(|u| u.name.clone()) on_close=close>
|
|
<p class="mt-1 text-ink/70">{user.with_value(|u| u.email.clone())}</p>
|
|
<ActionForm action=auth.logout attr:class="mt-6">
|
|
<button
|
|
type="submit"
|
|
class="w-full rounded-2xl border-2 border-ink px-4 py-3 font-semibold"
|
|
>
|
|
"Se déconnecter"
|
|
</button>
|
|
</ActionForm>
|
|
</Sheet>
|
|
</Show>
|
|
}
|
|
}
|