Skip to main content

sunday_slate/invites/
mod.rs

1mod extract;
2mod handlers;
3pub mod model;
4pub mod service;
5pub mod store;
6
7use crate::invites::model::InviteLink;
8use crate::{AppError, AppState};
9use axum::Router;
10use axum::routing::{get, post};
11use tower_sessions::Session;
12
13/// Session key holding the invite id the visitor most recently accepted, so
14/// team creation consumes that invite rather than the newest one for their
15/// email.
16const PENDING_INVITE_KEY: &str = "pending_invite_id";
17
18/// Session key carrying a just-minted accept link across the redirect to the
19/// invite list. A link cannot survive in the database — the secret is hashed —
20/// and putting it in the URL would leak it into history and logs, so it rides
21/// the session for exactly one render.
22const FRESH_LINK_KEY: &str = "fresh_invite_link";
23
24/// Remember the accepted invite for the coming team creation.
25pub(crate) async fn set_pending(session: &Session, invite_id: i64) -> Result<(), AppError> {
26    session.insert(PENDING_INVITE_KEY, invite_id).await?;
27    Ok(())
28}
29
30/// The accepted invite id, if any. Best-effort — a session error reads as
31/// none, and team creation falls back to the newest active invite.
32pub(crate) async fn pending(session: &Session) -> Option<i64> {
33    session.get(PENDING_INVITE_KEY).await.ok().flatten()
34}
35
36/// Hand a freshly minted link to the next render of the invite list.
37pub(crate) async fn set_fresh_link(
38    session: &Session,
39    invite_id: i64,
40    link: InviteLink,
41) -> Result<(), AppError> {
42    session.insert(FRESH_LINK_KEY, (invite_id, link)).await?;
43    Ok(())
44}
45
46/// The link stashed by the last create-or-copy, clearing it as it goes so a
47/// reload of the list does not keep showing it. Best-effort: a session error
48/// reads as none, and the list simply renders without a link.
49pub(crate) async fn take_fresh_link(session: &Session) -> Option<(i64, InviteLink)> {
50    session.remove(FRESH_LINK_KEY).await.ok().flatten()
51}
52
53/// Commissioner-only routes; mount under `login_required`.
54pub fn commissioner_router() -> Router<AppState> {
55    Router::new()
56        .route(
57            "/invites",
58            get(handlers::index).post(handlers::handle_create),
59        )
60        .route("/invites/new", get(handlers::new))
61        .route("/invites/{id}/link", post(handlers::link))
62        .route("/invites/{id}/revoke", post(handlers::revoke))
63}
64
65/// Public visitor routes; mount at session level (outside `login_required`).
66pub fn public_router() -> Router<AppState> {
67    Router::new()
68        .route("/invite", get(handlers::show))
69        .route("/invite/accept", get(handlers::accept))
70        .route(
71            "/invite/create-user",
72            get(handlers::create_user).post(handlers::handle_create_user),
73        )
74}