Create your custom store (untested, written from memory):
defmodule Plug.Session.PostgreSQL do
@behaviour Plug.Session.Store
@impl true
def init(opts), do: opts
@impl true
def get(conn, cookie, %{repo: repo, table: table, salt: salt}) do
with {:ok, sid} <- Phoenix.Token.verify(conn, salt, cookie),
session when not is_nil(session) <- repo.get(table, sid)
do
{sid, session.data}
else
_ -> {nil, %{}}
end
end
@impl true
def put(conn, sid, data, %{repo: repo, table: table, salt: salt}) do
session = %{id: sid, data: data}
with {1, [session]} <- repo.insert_all(table, [session], on_conflict: {:replace, [:data]}, returning: true) do
Phoenix.Token.sign(conn, salt, session.id)
else
_ -> ""
end
end
@impl true
def delete(_conn, sid, %{repo: repo, table: table}) do
repo.delete_all(from(s in table, where: s.id == ^sid))
:ok
end
end
And then in your controller:
- Create session:
put_session(conn, :user_id, uid) - Get session:
get_session(conn, :user_id) - Destroy session:
delete_session(conn, :user_id)


















