mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 04:27:59 +01:00
26 lines
510 B
Elixir
26 lines
510 B
Elixir
defmodule PhilomenaWeb.ApiTokenPlug do
|
|
alias Philomena.Users
|
|
alias Pow.Plug
|
|
|
|
def init([]), do: []
|
|
|
|
def call(conn, _opts) do
|
|
conn
|
|
|> maybe_find_user(conn.params["key"])
|
|
|> assign_user()
|
|
end
|
|
|
|
defp maybe_find_user(conn, nil), do: {conn, nil}
|
|
|
|
defp maybe_find_user(conn, key) do
|
|
user = Users.get_by(authentication_token: key)
|
|
|
|
{conn, user}
|
|
end
|
|
|
|
defp assign_user({conn, user}) do
|
|
config = Plug.fetch_config(conn)
|
|
|
|
Plug.assign_current_user(conn, user, config)
|
|
end
|
|
end
|