philomena/lib/philomena_web/plugs/api_token_plug.ex

25 lines
475 B
Elixir
Raw Normal View History

2019-12-01 00:11:24 +01:00
defmodule PhilomenaWeb.ApiTokenPlug do
alias Philomena.Users
alias Plug.Conn
2019-12-01 00:11:24 +01:00
def init([]), do: []
def call(conn, _opts) do
conn
|> maybe_find_user(conn.params["key"])
|> assign_user()
2019-12-01 00:11:24 +01:00
end
defp maybe_find_user(conn, nil), do: {conn, nil}
2020-01-11 05:20:19 +01:00
defp maybe_find_user(conn, token) do
user = Users.get_user_by_authentication_token(token)
2019-12-01 00:11:24 +01:00
{conn, user}
end
defp assign_user({conn, user}) do
Conn.assign(conn, :current_user, user)
2019-12-01 00:11:24 +01:00
end
2019-12-20 18:44:45 +01:00
end