Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with devise_token_auth and change_headers_on_each_request config #36

Open
bricesanchez opened this issue Nov 3, 2017 · 6 comments

Comments

@bricesanchez
Copy link

Hi @rmosolgo !

Thanks for your awesome job on GraphQL!

I'm trying to use GraphiQL rails with devise_token_auth gem and with have the change_headers_on_each_request option set to true. So i can't define the headers in the initializer like this :

GraphiQL::Rails.config.headers.merge! User.first.create_new_auth_token

Where could i define this config to be refreshed on every request?

@bricesanchez
Copy link
Author

my solution for now is to add a decorator on the GraphiQL::Rails::EditorsController :

# app/decorators/controllers/graphiql/rails/editors_controller_decorator.rb
module GraphiQLRailsEditorsControllerDecorator

  def self.prepended(base)
    base.prepend_before_action :authenticate, if: -> { !Rails.env.development? }
    base.prepend_before_action :set_auth_headers, only: :show
  end

  protected

  def authenticate
    http_basic_authenticate_with name: ENV["HBA_LOGIN"], password: ENV["HBA_PASSWORD"]
  end

  def set_auth_headers
    user = User.find_by_email(params[:email])

    if user.present?
       user_auth_token = user.create_new_auth_token

       GraphiQL::Rails.config.headers['access-token'] = -> (context) { user_auth_token['access-token'] }
       GraphiQL::Rails.config.headers['token-type'] = -> (context) { user_auth_token['token-type'] }
       GraphiQL::Rails.config.headers['client'] = -> (context) { user_auth_token['client'] }
       GraphiQL::Rails.config.headers['expiry'] = -> (context) { user_auth_token['expiry'] }
       GraphiQL::Rails.config.headers['uid'] = -> (context) { user_auth_token['uid'] }
    end
  end
end

GraphiQL::Rails::EditorsController.send :prepend, GraphiQLRailsEditorsControllerDecorator

@codingwaysarg
Copy link

Hi there!

Could you give me more details about your implementation? I'm trying to do the same thing so I can always get my user automatically authenticated while testing graphiql.

Thank you!

@abdul-shajin
Copy link

Could you give more info like an example or something?

@bricesanchez
Copy link
Author

Hi @codingwaysarg and @abdul-shajin,

I use devise_auth_token with this snippet of code. I can connect to the user by passing the email of User model as param, it's not suitable for production mode.

@basicBrogrammer
Copy link

Is this ever going to be merged in or do we fork?

@andredantasrocha
Copy link

Hi @bricesanchez,

It is an old post but I think this will be a solution for me as well. I am not sure how to use this module with Rails 6 and graphiql-rails 1.7 though...

I tried to put in app/decorators/controllers/graphiql/rails/editors_controller_decorator.rb but it wasn't pick up by Rails.

Then added it under initializers. In this case it was picked up but nothing happens. The GraphiQL::Rails::EditorsController remained unaffected 🤔

Any help is welcome 😄

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants