spaceship_helm/session

Types

A session with an ID and data store.

pub type Session {
  Session(id: String, data: dict.Dict(String, String))
}

Constructors

  • Session(id: String, data: dict.Dict(String, String))

Session storage backend.

pub type SessionStore {
  SessionStore(data: dynamic.Dynamic)
}

Constructors

Values

pub fn commit(
  resp: response.Response(body),
  session: Session,
  store: SessionStore,
) -> response.Response(body)

Save session and return the response.

This is a convenience function for saving session values:

let session = sessions.set_value(session, "username", "alice")
sessions.commit(response, session, store)
pub fn cookie(
  cookie_name: String,
  secret: String,
  store: SessionStore,
) -> fn(
  types.Context,
  fn(types.Context) -> response.Response(BitArray),
) -> response.Response(BitArray)

Create a cookie-based session middleware.

This middleware:

  1. Reads the session ID from the cookie
  2. Loads the session data from the store
  3. Makes the session available via sessions.get(ctx)
  4. Saves the session data when the response is sent

Arguments

  • cookie_name - Name of the session cookie (e.g., “session_id”)
  • _secret - Secret key for signing the session ID (reserved for future use)
  • store - Session storage backend
pub fn get(ctx: types.Context, f: fn(Session) -> a) -> a

Get the session from the context.

This function is used with use syntax in handlers:

use session <- sessions.get(ctx)
let username = sessions.get_value(session, "username")
pub fn get_value(
  session: Session,
  key: String,
) -> option.Option(String)

Get a value from the session.

pub fn new_store() -> SessionStore

Create a new in-memory session store.

pub fn set_value(
  session: Session,
  key: String,
  value: String,
) -> Session

Set a value in the session.

Returns a new session with the updated value.

Search Document