analytics/lib/plausible_web/plugs/auth_plug.ex
2020-03-31 15:38:42 +03:00

24 lines
521 B
Elixir

defmodule PlausibleWeb.AuthPlug do
import Plug.Conn
use Plausible.Repo
def init(options) do
options
end
def call(conn, _opts) do
case get_session(conn, :current_user_id) do
nil -> conn
id ->
user = Repo.get_by(Plausible.Auth.User, id: id)
|> Repo.preload(:subscription)
if user do
Sentry.Context.set_user_context(%{id: user.id, name: user.name})
assign(conn, :current_user, user)
else
conn
end
end
end
end