aalberti333

aalberti333

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!

Showing Posts 1 to 1

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews