2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.AuthPlug do
|
|
|
|
import Plug.Conn
|
2019-09-11 19:04:37 +03:00
|
|
|
use Plausible.Repo
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
def init(options) do
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _opts) do
|
|
|
|
case get_session(conn, :current_user_id) do
|
|
|
|
nil -> conn
|
|
|
|
id ->
|
2019-09-11 19:04:37 +03:00
|
|
|
user = Repo.get_by(Plausible.Auth.User, id: id)
|
|
|
|
|> Repo.preload(:subscription)
|
2019-09-02 14:29:19 +03:00
|
|
|
if user do
|
2020-02-17 14:22:31 +03:00
|
|
|
Sentry.Context.set_user_context(%{id: user.id, name: user.name})
|
2019-09-02 14:29:19 +03:00
|
|
|
assign(conn, :current_user, user)
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|