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

feat: T-911 handle JWT token expiration #10

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/concerns/authenticable_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def token

def decoded_token
@decoded_token ||= JWT.decode(token, Rails.application.secrets.secret_key_base, true, decode_options)
rescue JWT::DecodeError => e
raise e if e.is_a?(JWT::ExpiredSignature) || Rails.env.development?
end

def valid_token?
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class GraphqlController < ApplicationController
include AuthenticableUser
# If accessing from outside this domain, nullify the session
Expand All @@ -14,8 +16,11 @@ def execute
}
result = LagoApiSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
rescue JWT::ExpiredSignature
render_graphql_error(code: 'expired_jwt_token', status: 401)
rescue StandardError => e
raise e unless Rails.env.development?

handle_error_in_development(e)
end

Expand Down Expand Up @@ -47,4 +52,16 @@ def handle_error_in_development(e)

render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500
end

def render_graphql_error(code:, status:, message: nil)
render json: {
data: {},
errors: [
{
message: message || code,
extensions: { status: status, code: code }
}
]
}
end
end
1 change: 1 addition & 0 deletions app/graphql/types/error_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ErrorEnum < BaseEnum
value 'internal_error'
value 'unauthorized'
value 'token_encoding_error'
value 'expired_jwt_token'

value 'incorrect_login_or_password'
value 'user_already_exists'
Expand Down