Ecto Sessions

Database backend sessions with Ecto.

ecto_sessions in hex.pmecto_sessions documentation

ecto_sessions helps you easily and securely manage database backed sessions in your Ecto project.

It might be used, for example to manage authorization via cookies or API keys. The medium you will use the sessions is up to the application implementation. Ex: session id to be used in a Cookie or X-Api-Token for a REST API.

Using database backed session, might be very helpful in some scenarios. It has quite a few benefits and drawbacks comparing to signed sessions, for example JWT or signed cookies. It might also be used in combination with them.

Advantages:

Disadvantages:

One design that allows you to have the benefits of stateless and stateful sessions combined, is to have short-lived signed tokens, and database backed sessions for long-lived refresh tokens.

Demo

This repository includes a full phoenix demo project inside the ./demo directory. There’s a hosted live version available at ecto-sessions-demo.tofran.com, feel free to play with it and see how it works/what features it enables.

Installation

The package can be installed by adding ecto_sessions to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_sessions, "> 0.0.0"}
  ]
end

Then, in your ecto app create the following module:

defmodule MyApp.Sessions do
  use EctoSessions,
    repo: MyApp.Repo
end

Refer to EctoSessions module documentation for more details.