ueberauth / guardian

Elixir Authentication
MIT License
3.44k stars 381 forks source link

Guardian 1.0 - ** (RuntimeError) `error_handler` not set in Guardian pipeline #496

Closed bdensmore closed 6 years ago

bdensmore commented 6 years ago

Hi!

I'm trying to implement Guardian into an API I'm working on and whenever I make a call to an endpoint I'm getting the error ** (RuntimeError) error_handler not set in Guardian pipeline.

I'm only trying to hit the /sign_up route right now for testing this out. My router is set up like so:

defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  pipeline :api do
    plug :accepts, ["json"]
    plug Guardian.Plug.VerifyHeader
    plug Guardian.Plug.LoadResource
  end

  pipeline :authenticated do
    plug Guardian.Plug.EnsureAuthenticated
  end

  scope "/api", MyAppWeb do
    pipe_through :api
    post "/sign_in", SessionController, :sign_in
    post "/sign_up", RegistrationController, :sign_up

    pipe_through :authenticated
     resources "/users", UserController, except: [:new, :edit]
     resources "/availabilities", AvailabilityController, except: [:new, :edit]
     resources "/leads", LeadController, except: [:edit, :new]
     resources "/clients", ClientController, except: [:edit, :new]
  end

end

My RegistrationController looks like:

defmodule MyAppWeb.RegistrationController do
  use MyAppWeb, :controller

  alias MyApp.Auth
  alias MyApp.Auth.User

  action_fallback MyApp.FallbackController

  def sign_up(conn, %{"user" => user_params}) do
    with {:ok, %User{} = user} <- Auth.create_user(user_params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", user_path(conn, :show, user))
      |> render("success.json", user: user)
    end
  end
end

Is there somewhere I should be defining an error_handler for Guardian to use? In the router I did try doing:

plug Guardian.Plug.LoadResource, error_handler: MyAppWeb.ErrorHandlerView but that leads to a different error saying ErrorHandlerView is private.

Any help would be greatly appreciated.

Thank you, Ben

ghost commented 6 years ago

Did you define a guardian pipeline and configure it correctly in the config? You can find an example for an API with Guardian on my github account or additional infos in the documentation.

hassox commented 6 years ago

There's a bit more information in the guides. We haven't pushed the release so it's not available on hex yet but you can access it on github: https://github.com/ueberauth/guardian/blob/master/guides/plug/pipelines.md

bdensmore commented 6 years ago

@hlhr Thanks for the link to the example, that was extremely beneficial for me. I was still setting things up pre 1.0 which caused the issues I was having. Using your example app helped me to get things working.

@hassox Thank you for the added info in the guide as well.

Thank you both!

Ben