简体   繁体   中英

Sending a graphql response from Guardian error handler when using Absinthe

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM