Sha256: 7b97920f600edb26d8cba74815bcc609b8ed2447ca4c4bdfe10fc01e257b4a41

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module SolidusJwt
  module Spree
    module Api
      module BaseControllerDecorator
        def self.prepended(base)
          base.rescue_from JWT::DecodeError do
            render "spree/api/errors/invalid_api_key", status: :unauthorized
          end
        end

        ##
        # Overrides Solidus
        # @see https://github.com/solidusio/solidus/blob/master/api/app/controllers/spree/api/base_controller.rb
        #
        def load_user
          return super if json_web_token.blank?

          # rubocop:disable Naming/MemoizedInstanceVariableName
          @current_api_user ||= ::Spree.user_class.find_by(id: json_web_token['id'])
          # rubocop:enable Naming/MemoizedInstanceVariableName
        end

        def json_web_token
          @json_web_token ||= SolidusJwt.decode(api_key).first
        rescue JWT::DecodeError
          # Allow spree to try and authenticate if we still allow it. Otherwise
          # raise an error
          return if SolidusJwt::Config.allow_spree_api_key

          raise
        end

        if SolidusSupport.api_available?
          ::Spree::Api::BaseController.prepend self
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_jwt-1.1.0 app/decorators/solidus_jwt/spree/api/base_controller_decorator.rb