aalberti333

aalberti333

'error_handler' not set in Guardian pipeline

I’m trying to set up user authentication with Guardian 1.0 for my Phoenix 1.4 Absinthe api. Upon launching the server, I’m confronted with the following error from Graphiql:

# RuntimeError at POST /api

Exception:

    ** (RuntimeError) `error_handler` not set in Guardian pipeline
        (guardian) lib/guardian/plug/pipeline.ex:233: Guardian.Plug.Pipeline.raise_error/1

where my terminal output is:

[error] pid<0.496.0> running MyAppWeb.Endpoint (connection pid<0.492.0>, stream id 2) terminated
Server: localhost:4000 (http)                                                                                     
Request: POST /api                                                                                                
** (exit) an exception was raised:                                                                                
   ** (RuntimeError) error_handler not set in Guardian pipeline                                                
       (guardian) lib/guardian/plug/pipeline.ex:233: Guardian.Plug.Pipeline.raise_error/1                        
       (guardian) lib/guardian/plug/load_resource.ex:77: Guardian.Plug.LoadResource.return_error/3               
       (my_app) lib/my_app_web/router.ex:12: MyAppWeb.Router.graphql/2                                           
       (my_app) lib/my_app_web/router.ex:1: MyAppWeb.Router.pipe_through1/1                                  
       (phoenix) lib/phoenix/router.ex:270: Phoenix.Router.call/1                                            
       (my_app) lib/my_app_web/endpoint.ex:1: MyAppWeb.Endpoint.plug_builder_call/2                              
       (my_app) lib/plug/debugger.ex:122: MyAppWeb.Endpoint.“call (overridable 3)”/2                             
       (my_app) lib/my_app_web/endpoint.ex:1: MyAppWeb.Endpoint.call/2                                           
       (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:34: Phoenix.Endpoint.Cowboy2Handler.init/2              
       (cowboy) /home/spotless/Documents/AbsintheBook/OnlineTutorialBasics/tutorial/my_app/deps/cowboy/src/cowbo
y_handler.erl:41: :cowboy_handler.execute/2                                                                       
       (cowboy) /home/spotless/Documents/AbsintheBook/OnlineTutorialBasics/tutorial/my_app/deps/cowboy/src/cowbo
y_stream_h.erl:296: :cowboy_stream_h.execute/3                                                                    
       (cowboy) /home/spotless/Documents/AbsintheBook/OnlineTutorialBasics/tutorial/my_app/deps/cowboy/src/cowbo
y_stream_h.erl:274: :cowboy_stream_h.request_process/3                                                            
       (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3  

I’ve tried including the following line in my :graphql pipeline

plug Guardian.Plug.Pipeline, module: MyApp.Guardian, error_handler: Guardian.Plug.ErrorHandler

but I have not been successful. I’ve been following a tutorial which uses Guardian .14 and Phoenix 1.2, and I’ve been trying to transcribe different pieces of it with help from this community in previous posts. This is the last piece of the tutorial I’ll need help with to get it fully functional, and any help would be greatly appreciated :slight_smile: I’ll leave relevant pieces of code below:

/lib/my_app_web/router.ex

defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :graphql do
    # plug Guardian.Plug.Pipeline, module: MyApp.Guardian, error_handler: Guardian.Plug.ErrorHandler
    plug Guardian.Plug.VerifyHeader, realm: "Bearer"
    plug Guardian.Plug.LoadResource
    plug MyAppWeb.Context
  end

  scope "/", MyAppWeb do
    pipe_through :browser

    get "/", PageController, :index
  end

  scope "/api" do
    pipe_through :graphql

    forward "/", Absinthe.Plug,
      schema: MyAppWeb.Schema
  end

  forward "/graphiql", Absinthe.Plug.GraphiQL,
    schema: MyAppWeb.Schema

end

/lib/my_app/guardian.ex

defmodule MyApp.Guardian do
  use Guardian, otp_app: :my_app

  alias MyApp.Repo
  alias MyApp.User

  def subject_for_token(user=%User{}, _claims) do
    {:ok, "User:#{to_string(user.id)}"}
  end

  def resource_from_claims(claims) do
    "User: " <> user_id = claims["sub"]
    case Repo.get(User, user_id) do
      %User{} = user -> {:ok, user}
      _ -> {:error, :reason_for_error}
    end
  end
end

/lib/my_app_web/plugs/context.ex

defmodule MyAppWeb.Context do
  @behaviour Plug

  import Plug.Conn

  def init(opts), do: opts

  def call(conn, _) do
    case Guardian.Plug.current_resource(conn) do
      nil -> conn
      user -> put_private(conn, :absinthe, %{context: %{current_user: user}})
    end
  end
end

Thanks in advance for any feedback!

Most Liked

romseguy

romseguy

Have you seen this guide guardian/upgrade_guides/0.14.to.1.0.md at master · ueberauth/guardian · GitHub ?

I haven’t upgraded myself (still at 0.14.4) but planning to do so soon since updating elixir/phoenix/absinthe to 1.8.1/1.4.1/1.4.16 broke the context plug Guardian.Plug.current_resource(conn) returns nil even after login.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement