Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pierreabreup/4cb4c5bb27927533280fb6efde37827f to your computer and use it in GitHub Desktop.
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
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