From d1f527c3ac043f80e8cb65d0e67804e6911e68b1 Mon Sep 17 00:00:00 2001 From: Kevin Hughes Date: Thu, 26 Jul 2018 23:33:56 +0200 Subject: [PATCH] fix graphiql for jwt auth --- app/controllers/admin_controller.rb | 10 ++++++++++ config/initializers/graphiql.rb | 1 + 2 files changed, 11 insertions(+) create mode 100644 config/initializers/graphiql.rb diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index f340c62b6..b0a887f17 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -9,6 +9,7 @@ class AdminController < ApplicationController before_action :authenticate_user! before_action :authenticate_tournament_user! + before_action :set_jwt_cookie rescue_from(ActiveRecord::RecordNotFound, with: :render_admin_404) @@ -18,6 +19,15 @@ def authenticate_tournament_user! end end + def set_jwt_cookie + token = Knock::AuthToken.new(payload: { sub: current_user.id }).token + + cookies['jwt'] = { + value: token, + domain: :all + } + end + def execute_graphql(mutation, input_type, input, output) query_string = "mutation #{mutation}($input: #{input_type}!) {#{mutation}(input: $input) #{output}}" query_variables = {"input" => input.deep_transform_keys { |key| key.to_s.camelize(:lower) }} diff --git a/config/initializers/graphiql.rb b/config/initializers/graphiql.rb new file mode 100644 index 000000000..9b98f6573 --- /dev/null +++ b/config/initializers/graphiql.rb @@ -0,0 +1 @@ +GraphiQL::Rails.config.headers['Authorization'] = -> (context) { "bearer #{context.cookies['jwt']}" }