module Auth0CurrentUser::Secured

Private Instance Methods

auth_token() click to toggle source
# File lib/auth0_current_user/secured.rb, line 33
def auth_token
  JsonWebToken.verify(http_token)
end
authenticate_request!() click to toggle source
# File lib/auth0_current_user/secured.rb, line 18
def authenticate_request!
  token = auth_token
  set_current_user(token)

  token
rescue JWT::VerificationError, JWT::DecodeError
  render json: { errors: ['Not Authenticated'] }, status: :unauthorized
end
authenticated_klass() click to toggle source
# File lib/auth0_current_user/secured.rb, line 50
def authenticated_klass
  unless configuration.authenticated_klass
    raise NotImplementedError, 'You must define the #authenitcated_klass in config/initializers/auth0_current_user'
    return
  end
  
  @authenticated_klass ||= configuration.authenticated_klass.to_s.classify
rescue StandardError => e
  Rails.logger.error(e.message)
end
configuration() click to toggle source
# File lib/auth0_current_user/secured.rb, line 61
def configuration
  @configuration ||= Configuration.new
end
current_user() click to toggle source
# File lib/auth0_current_user/secured.rb, line 46
def current_user
  @current_user ||= RequestStore.store[:current_user]
end
get_email(token) click to toggle source
# File lib/auth0_current_user/secured.rb, line 37
def get_email(token)
  JsonWebToken.get_claim(token, 'email')
end
http_token() click to toggle source
# File lib/auth0_current_user/secured.rb, line 27
def http_token
  if request.headers['Authorization'].present?
    request.headers['Authorization'].split(' ').last
  end
end
set_current_user(token) click to toggle source
# File lib/auth0_current_user/secured.rb, line 41
def set_current_user(token)
  email = get_email(token)
  RequestStore.store[:current_user] ||= Kernel.const_get(authenticated_klass).find_by(email: email)
end