View Source Charon.Models.Session (Charon v3.2.0)
A session.
Summary
Functions
Upgrade a session (or map created from a session struct) to the latest struct version (7).
Types
@type t() :: %Charon.Models.Session{ created_at: integer(), expires_at: integer() | :infinite, extra_payload: map(), id: String.t(), lock_version: integer(), prev_tokens_fresh_from: integer(), refresh_expires_at: integer(), refresh_token_id: binary(), refreshed_at: integer(), tokens_fresh_from: integer(), type: atom(), user_id: pos_integer() | binary(), version: pos_integer() }
Functions
@spec upgrade_version(map(), Charon.Config.t()) :: map()
Upgrade a session (or map created from a session struct) to the latest struct version (7).
DocTests
@charon_config Charon.Config.from_enum(token_issuer: "local")
# old version without the :version, :refesh_expires_at fields but with :__struct__ set
# is updated to latest version (7)
iex> session = %{
...> __struct__: Session,
...> created_at: 0,
...> expires_at: 1,
...> extra_payload: %{},
...> id: "ab",
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> type: :full,
...> user_id: 9
...> }
...> |> upgrade_version(@charon_config)
iex> %Session{
...> created_at: 0,
...> expires_at: 1,
...> extra_payload: %{},
...> id: "ab",
...> prev_tokens_fresh_from: 0,
...> refresh_expires_at: 1,
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> tokens_fresh_from: 15,
...> type: :full,
...> user_id: 9,
...> version: 7
...> } = session
# old version - with :expires_at = nil - is updated without error
iex> session = %{
...> __struct__: Session,
...> created_at: 0,
...> expires_at: nil,
...> extra_payload: %{},
...> id: "ab",
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> type: :full,
...> user_id: 9
...> }
...> |> upgrade_version(@charon_config)
iex> %Session{
...> created_at: 0,
...> expires_at: :infinite,
...> extra_payload: %{},
...> id: "ab",
...> prev_tokens_fresh_from: 0,
...> refresh_expires_at: refresh_exp,
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> tokens_fresh_from: 15,
...> type: :full,
...> user_id: 9,
...> version: 7
...> } = session
iex> is_integer(refresh_exp) and refresh_exp >= System.os_time(:second)
true