Hcs.Plug.Session

hcs · API reference

Cookie-based session management with fiber-local isolation.

  let store = Session.Memory_store.create () in
  let session_plug = Session.create ~store () in

  let handler req =
    match Session.get "user_id" with
    | Some uid -> Session.put "last_seen" (string_of_float now); ...
    | None -> ...
type session = {
  id : string;
  data : (string * string) list;
  created_at : float;
}
type store = 
  | Memory of (string, session) Kcas_data.Hashtbl.t
  | Cookie of {
    secret : string;
    max_age : float;
  }
type session_ctx = {
  mutable data : (string * string) list;
  mutable modified : bool;
}
val session_key : session_ctx Eio.Fiber.key
val get_ctx : unit -> session_ctx
module Memory_store : sig ... end
val get : string -> string option
val get_with_req : 'a -> string -> string option
val put : string -> string -> unit
val put_with_req : 'a -> string -> string -> unit
val delete : string -> unit
val delete_with_req : 'a -> string -> unit
val clear : unit -> unit
val clear_with_req : 'a -> unit
val get_all : unit -> (string * string) list
val get_all_with_req : 'a -> (string * string) list
type config = {
  cookie_name : string;
  store : store;
  secure : bool;
  same_site : [ `Strict | `Lax | `None ];
  http_only : bool;
  path : string;
  max_age : float;
}
val generate_id : unit -> string
val create : 
  store:store ->
  ?cookie_name:string ->
  ?secure:bool ->
  ?same_site:[ `Lax | `None | `Strict ] ->
  ?http_only:bool ->
  ?path:string ->
  ?max_age:float ->
  unit ->
  (Server.request -> Server.response) ->
  Server.request ->
  Server.response

Create session plug with configurable storage and cookie options.