add jwt authentication on graphql controller

This commit is contained in:
João Geonizeli
2021-08-04 18:49:11 -03:00
parent 64a7fc7da9
commit a755945c61
7 changed files with 78 additions and 11 deletions

View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
module Auth
class Auth0Client
class << self
def find_profile(token)
Profile.new(user_profile_attributes(token))
end
def user_profile_attributes(token)
HTTParty.get(
"https://#{ENV["AUTH_DOMAIN"]}/userinfo",
headers: {
"Content-Type" => "application/json",
"Authorization": "Bearer #{token}",
}
).with_indifferent_access
end
end
end
end

View File

@@ -0,0 +1,14 @@
# frozen_string_literal: true
module Auth
class Authenticate
attr_reader :jwt_token
def initialize(jwt_token)
@jwt_token = jwt_token
end
def profile
Auth0Client.find_profile(jwt_token)
end
end
end

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
module Auth
class Profile
attr_reader :id, :email
def initialize(attributes)
@id = attributes[:sub]
@email = attributes[:email]
end
def customer
@customer ||= Customer.find_by(email: email, auth_id: id)
end
end
end