View Source EctoSessions behaviour (Ecto Sessions v0.1.0)

This lib implements a set of methods to help you handle the storage and access to databse-backend sessions.

In your application, use EctoSessions:

defmodule MyApp.Sessions do
  use EctoSessions,
    repo: MyApp.Repo, # required
    prefix: nil, # optional
    table_name: "sessions", # optional
    extra_fields: [user_id: :string] # optional

See EctoSessions.Migrations for instructions on how to migrate your database.

Link to this section Summary

Callbacks

Creates a session using create_session!/1 returing only the plaintext auth token.

Same as create_session/1 but using Ecto.Repo.insert!/2.

Creates a session. attrs is a map that contains EctoSessions.Session attributes, where the keys are atoms.

Filters a sessions query by the given field.

Returns an ecto query for sessions which have expired: Whenever expires_at is in the past.

Same as get_session/2 but using Ecto.Repo.one!/1.

Returns a valid session given the field name and the desired value to check.

Returns an ecto query for sessions which have not expired: Whenever expires_at is either null or in the future.

Renovates a session expiration following the configuration in EctoSession.Config.

Link to this section Callbacks

Link to this callback

create_auth_token(attrs)

View Source
@callback create_auth_token(attrs :: map()) :: Ecto.Schema.t()

Creates a session using create_session!/1 returing only the plaintext auth token.

examples

Examples

iex> create_session(%{user_id: "1234", data: %{device_name: "Samle Browser"}})
"plaintext-auth-token"
@callback create_session!(attrs :: map()) :: Ecto.Schema.t()

Same as create_session/1 but using Ecto.Repo.insert!/2.

@callback create_session(attrs :: map()) :: Ecto.Schema.t()

Creates a session. attrs is a map that contains EctoSessions.Session attributes, where the keys are atoms.

Uses Ecto.Repo.insert/2

examples

Examples

iex> create_session(%{user_id: "1234", data: %{device_name: "Samle Browser"}})
%Session{
  user_id: "1234",
  data: %{
    device_name: "Sample Browser",
    plaintext_auth_token: "plaintext-auth-token"
    auth_token: "hashed-token"
  }
}
Link to this callback

filter_session_query_by(query, field_name, value)

View Source
@callback filter_session_query_by(
  query :: Ecto.Queryable.t(),
  field_name :: atom(),
  value :: any()
) ::
  Ecto.Queryable.t()

Filters a sessions query by the given field.

When :auth_token is passed, hashing will be automatically handled according to the configuration.

Link to this callback

get_expired_sessions_query()

View Source
@callback get_expired_sessions_query() :: Ecto.Queryable.t()

Returns an ecto query for sessions which have expired: Whenever expires_at is in the past.

Link to this callback

get_session!(field_name, value)

View Source
@callback get_session!(field_name :: atom(), value :: any()) :: Ecto.Schema.t()

Same as get_session/2 but using Ecto.Repo.one!/1.

Link to this callback

get_session(field_name, value)

View Source
@callback get_session(field_name :: atom(), value :: any()) :: Ecto.Schema.t()

Returns a valid session given the field name and the desired value to check.

Uses Ecto.Repo.one/1

Link to this callback

get_valid_sessions_query()

View Source
@callback get_valid_sessions_query() :: Ecto.Queryable.t()

Returns an ecto query for sessions which have not expired: Whenever expires_at is either null or in the future.

Link to this callback

renovate_session!(session)

View Source
@callback renovate_session!(session :: Ecto.Schema.t()) :: Ecto.Schema.t()

Renovates a session expiration following the configuration in EctoSession.Config.