Last active
May 11, 2020 22:00
-
-
Save pierreabreup/4cb4c5bb27927533280fb6efde37827f to your computer and use it in GitHub Desktop.
This code shows you how to render a phoenix error view inside a Plug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyAppWeb.Plug.Authorization do | |
@behaviour Plug | |
import Plug.Conn | |
def init(opts), do: opts | |
def call(conn, _) do | |
with {:ok, token} <- token(conn), | |
{:ok, current_user} <- authorize(token) do | |
assign(conn, :current_user, current_user) | |
else | |
_ -> | |
conn | |
|> put_status(401) | |
|> Phoenix.Controller.put_view(MyAppWeb.ErrorView) | |
|> Phoenix.Controller.render(:"401", %{}) | |
|> halt() | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment