Sending a graphql response from Guardian error handler when using Absinthe

270 views Asked by At

I have a Phoenix app with Absinthe for a Graphql API.

Guardian is used to authenticate requests that provide a Bearer token in the header. This is all working great until an invalid token is provided. I have specified an error handler in my Guardian pipeline and it currently just responds with a 401 http response:

def auth_error(conn, {type, _reason}, _opts) do
    body = to_string(type)

    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(401, body)
end

This is not friendly for any graphql client and my resolvers don't really care if the token is invalid as they have their own checks to see if the user is provided in the context.

Is there a way to continue the pipeline from the error handler if the token is invalid so I can just give a proper graphql response?

1

There are 1 answers

0
Marcos Tapajós On

My suggestion is creating a Plug that will validate the Bearer token and set the current user on the context. Then you can write an Absinthe.Middleware to verify the current user presence and set an error if it is not present.

Also, take a look on blunder-absinthe for better errors.