Sha256: 3686e70c591df97c71a4aa8322c8713e6babc2c22c0312b0ce8cdda9d098b597

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

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.for_jwt(json_web_token['sub'] || 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

3 entries across 3 versions & 1 rubygems

Version Path
solidus_jwt-1.2.2 app/decorators/controllers/solidus_jwt/spree/api/base_controller_decorator.rb
solidus_jwt-1.2.1 app/decorators/controllers/solidus_jwt/spree/api/base_controller_decorator.rb
solidus_jwt-1.2.0 app/decorators/controllers/solidus_jwt/spree/api/base_controller_decorator.rb