hermet hermet SSR. Islands. Rust.

Login and members-only pages

Hermet provides sessions, which keep a user signed in after you have verified who they are. Your site decides how to verify users and which member type to give each user.

Turning on sessions

src/manifest.rs
pub const REGULAR: Member = Member::new("regular");   // member types
pub const VIP: Member = Member::new("vip");

App::new(hermet::generated!())
    .sessions(Sessions::kv("SESSIONS").members([REGULAR, VIP]))   // sessions are stored in KV (SESSIONS)
    .login("/login")                                               // where to go when login is required

Add the KV for sessions to the configuration (in cloudflare.rs, with Worker::kv) under the name SESSIONS. Hermet takes care of unguessable IDs, a cookie that JavaScript cannot read (HttpOnly, with the __Host- prefix in its name), expiry in KV, a new ID on every login, and deletion on logout. The cookie does not contain the user ID itself.

Login and logout

In a form handler
// Login: after you verify the user, choose the member type and start a session
let cookie = deps.sessions().start(User { id }, VIP, &[]).await?;
Ok(Redirect::to(next).set_cookie(cookie))

// Logout
let cookie = deps.sessions().end().await?;
  • Always check the return destination after login (next) with LocalUrl::new. Otherwise, it becomes a hole that sends users to other sites.
  • The member type is a snapshot taken at login. When a user's member type changes or their contract ends, revoke all of that user's sessions with deps.sessions().revoke_user(&User { id }).
  • A change in KV usually takes about 60 seconds to reach every data center (there is no guaranteed upper bound). For use cases that need immediate, guaranteed forced logout, this alone is not sufficient.

Members-only pages

src/manifest.rs
.page(Page::at("/members", members::page()).cache(Cache::Ttl(600)).only_for([REGULAR, VIP]))

The Gate decides before it calls the Renderer. People who are not signed in are sent to the login page (302). Users of the wrong member type get 403. If valid shared HTML is in the cache, it is reused among the members who may view the page.

Different content per member type (per_member)

src/pages/offers.rs
pub fn page() -> MemberPageBody<OffersView> { PageBody::per_member(load) }   // offers.html is a whole HTML document

pub async fn load(_shared: Shared, _params: RouteParams, member: Member) -> Result<Option<OffersView>> {
    let list = match member {
        m if m == VIP => Markup::of(&VipOffers),
        _ => Markup::of(&RegularOffers),
    };
    Ok(Some(OffersView { list }))
}
  • The cache holds one entry per member type.
  • per_member can be used only together with only_for. Write Page::at(…).only_for([…]).cache(…) in that order.
  • Only the member type name reaches the Renderer, never who the user is. Write per-user content in Server Islands.
Pricing and limits

Each session check reads three KV keys. On the free plan, once reads exceed the daily limit, even correctly signed-in users cannot open members-only pages until the reset (00:00 UTC each day). On a paid plan, reads beyond the included amount are billed. These values can change, so check Workers KV pricing.