View Source Charon.Models.Session (Charon v1.3.4)

A session.

Link to this section Summary

Functions

Deserialize a session, without breaking for structural changes in the session struct.

Create a new session from config values and overrides.

Serialize a session.

Link to this section Types

@type t() :: %Charon.Models.Session{
  created_at: integer(),
  expires_at: integer() | :infinite,
  extra_payload: map(),
  id: String.t(),
  refresh_token_id: String.t(),
  refreshed_at: integer(),
  type: atom(),
  user_id: pos_integer() | binary(),
  version: pos_integer()
}

Link to this section Functions

@spec deserialize(binary()) :: struct()

Deserialize a session, without breaking for structural changes in the session struct.

doctests

DocTests

@charon_config Charon.Config.from_enum(token_issuer: "local")

# serialization is reversible
iex> %Session{} = @charon_config |> new() |> serialize() |> deserialize()

# old version - without the :version field but with :__struct__ set - is deserialized without error
iex> %{__struct__: Session, created_at: 0, id: "ab", user_id: 9, expires_at: 1, refresh_token_id: "cd", refreshed_at: 0, type: :full, extra_payload: %{}}
...> |> :erlang.term_to_binary()
...> |> deserialize()
%Session{created_at: 0, id: "ab", user_id: 9, expires_at: 1, refresh_token_id: "cd", refreshed_at: 0, type: :full, extra_payload: %{}, version: 1}

# old version - with :expires_at = nil - is deserialized without error
iex> %Session{expires_at: :infinite} = @charon_config |> new(expires_at: nil) |> serialize() |> deserialize()
Link to this function

new(config, overrides \\ [])

View Source
@spec new(Charon.Config.t(), keyword() | map()) :: t()

Create a new session from config values and overrides.

Provides defaults for :id, :created_at and :expires_at.

@spec serialize(struct()) :: binary()

Serialize a session.