Skip to content

Commit 46a878b

Browse files
Merge pull request #3 from matthewsullivan/feature/use-token-in-request-header
Feature/use request authorization header
2 parents 678827e + 52fa30f commit 46a878b

File tree

6 files changed

+18
-27
lines changed

6 files changed

+18
-27
lines changed

app/controllers/graphql_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ def execute
55
variables = prepare_variables(params[:variables])
66
query = params[:query]
77
operation_name = params[:operationName]
8-
token = variables&.dig(:input, :arguments, :token)
9-
context = { current_user: current_user(token) }
8+
context = { current_user: current_user }
109
result = RubyJwtPostgresAuthSchema.execute(
1110
query,
1211
variables: variables,
@@ -22,8 +21,9 @@ def execute
2221

2322
private
2423

25-
def current_user(token)
26-
return unless token
24+
def current_user
25+
token = request.headers['Authorization']&.split(' ')&.last
26+
return if token.blank?
2727

2828
JwtHelper.logged_in_user(token)
2929
end

app/graph/user/mutations/update_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def resolve(arguments:)
1010
current_user = context[:current_user]
1111
raise StandardError unless current_user
1212

13-
current_user.update!(arguments.to_hash.except!(:token))
13+
current_user.update!(arguments.to_hash)
1414
{ user: current_user }
1515
rescue ActiveRecord::RecordInvalid => e
1616
GraphQL::ExecutionError.new("Invalid input: #{e.record.errors.full_messages.join(', ')}")

app/graph/user/types/input/fetch_user.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ class FetchUser < Base::Types::BaseInputObject
55
description 'Input for fetching a user'
66

77
argument :id, String, required: true
8-
argument :token, String, required: true
98
end
109
end

app/graph/user/types/input/update_user.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ class UpdateUser < Base::Types::BaseInputObject
88
argument :first_name, String, required: false
99
argument :last_name, String, required: false
1010
argument :password, String, required: false
11-
argument :token, String, required: true
1211
end
1312
end

test/graph/mutations/update_user_test.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ def perform(args = {})
2020
}
2121
}
2222
GRAPHQL
23-
post '/graph', params: { query: query, variables: args }
23+
post '/graph', params: { query: query, variables: args }, headers: { 'HTTP_AUTHORIZATION' => "Bearer: #{@token}" }
2424
JSON.parse(@response.body)
2525
end
2626

2727
test 'should not update without valid token' do
2828
parameters = {
2929
input: {
3030
arguments: {
31-
email: 'janedoe@localhost.com',
32-
token: ''
31+
email: 'janedoe@localhost.com'
3332
}
3433
}
3534
}
35+
@token = ''
3636
result = perform(parameters)
3737

3838
assert_equal('Must be logged in to access requested resource', result['errors'][0]['message'])
@@ -45,8 +45,7 @@ def perform(args = {})
4545
firstName: 'Jonathan',
4646
lastName: 'D.',
4747
email: 'jonathandoe@localhost.com',
48-
password: '!a1B2c3D4e5F6g!',
49-
token: @token
48+
password: '!a1B2c3D4e5F6g!'
5049
}
5150
}
5251
}
@@ -62,8 +61,7 @@ def perform(args = {})
6261
parameters = {
6362
input: {
6463
arguments: {
65-
firstName: '',
66-
token: @token
64+
firstName: ''
6765
}
6866
}
6967
}
@@ -76,8 +74,7 @@ def perform(args = {})
7674
parameters = {
7775
input: {
7876
arguments: {
79-
lastName: '',
80-
token: @token
77+
lastName: ''
8178
}
8279
}
8380
}
@@ -90,8 +87,7 @@ def perform(args = {})
9087
parameters = {
9188
input: {
9289
arguments: {
93-
email: '',
94-
token: @token
90+
email: ''
9591
}
9692
}
9793
}
@@ -104,8 +100,7 @@ def perform(args = {})
104100
parameters = {
105101
input: {
106102
arguments: {
107-
email: 'janedoe@localhost.com',
108-
token: @token
103+
email: 'janedoe@localhost.com'
109104
}
110105
}
111106
}

test/graph/queries/fetch_user_test.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def perform(args = {})
2020
}
2121
}
2222
GRAPHQL
23-
post '/graph', params: { query: query, variables: args }
23+
post '/graph', params: { query: query, variables: args }, headers: { 'HTTP_AUTHORIZATION' => "Bearer: #{@token}" }
2424
JSON.parse(@response.body)
2525
end
2626

@@ -30,8 +30,7 @@ def perform(args = {})
3030
parameters = {
3131
input: {
3232
arguments: {
33-
id: encoded_id,
34-
token: @token
33+
id: encoded_id
3534
}
3635
}
3736
}
@@ -47,11 +46,11 @@ def perform(args = {})
4746
parameters = {
4847
input: {
4948
arguments: {
50-
id: 'A1b2C3d4',
51-
token: ''
49+
id: 'A1b2C3d4'
5250
}
5351
}
5452
}
53+
@token = ''
5554
result = perform(parameters)
5655

5756
assert_equal('Must be logged in to access requested resource', result['errors'][0]['message'])
@@ -61,8 +60,7 @@ def perform(args = {})
6160
parameters = {
6261
input: {
6362
arguments: {
64-
id: '124',
65-
token: @token
63+
id: 'A1b2C3d4'
6664
}
6765
}
6866
}

0 commit comments

Comments
 (0)