mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-18 06:57:59 +01:00
25 lines
509 B
Elixir
25 lines
509 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
|