Packages
integratedb
0.1.0
IntegrateDB is a database sharing system. It provides integration primitives and data ownership and migration controls. Use it to integrate applications directly through a Postgres database.
Current section
Files
Jump to
Current section
Files
lib/integrate_web/plugs/auth_required_unless_no_users_plug.ex
defmodule IntegrateWeb.AuthRequiredUnlessNoUsersPlug do
@moduledoc """
Ensures that we have a `conn.assigns[:current_user]`.
If not, returns a 401 response with a JSON error payload.
**Unless** there are no users yet -- which is the case when creating
the first root user.
"""
@behaviour Plug
alias Integrate.Accounts
alias IntegrateWeb.AuthRequiredPlug
def init(opts), do: opts
def call(conn, _opts) do
conn.assigns
|> Map.get(:current_user)
|> authorize(conn)
end
def authorize(nil, conn) do
case Accounts.any_users_exist?() do
true ->
AuthRequiredPlug.authorize(nil, conn)
false ->
conn
end
end
def authorize(_user, conn) do
conn
end
end