Sha256: 170b7413ac0f09ad21d20fc3f7449f1064c8b333b4822ce3f50b5bf2f3ad972a

Contents?: true

Size: 1013 Bytes

Versions: 45

Compression:

Stored size: 1013 Bytes

Contents

module Ecom
  module Core
    class ApplicationController < ActionController::API
      before_action :authenticate

      def logged_in?
        !current_user.nil?
      end

      def current_user
        return unless auth_present?

        user = User.find(auth['id'])
        @current_user ||= user if user
      end

      private

      def authenticate
        render json: { error: 'Unauthorized' }, status: 401 unless logged_in?
      end

      def token
        return nil if request.env['HTTP_AUTHORIZATION'].nil?

        request.env['HTTP_AUTHORIZATION'].scan(/Bearer (.*)$/).flatten.last
      end

      def auth
        TokenAuthService.decode(token)
      end

      def auth_present?
        !token.nil?
      end

      def generate_error_message(entity)
        value = entity.errors.details.first[1][0][:value]
        field = entity.errors.keys[0]
        message = entity.errors.values[0][0]

        entity.errors.full_message(field, "'#{value}' #{message}")
      end
    end
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
ecom_core-1.3.14 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.13 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.12 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.11 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.10 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.9 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.8 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.7 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.6 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.5 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.4 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.3 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.2 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.1 app/controllers/ecom/core/application_controller.rb
ecom_core-1.3.0 app/controllers/ecom/core/application_controller.rb
ecom_core-1.2.36 app/controllers/ecom/core/application_controller.rb
ecom_core-1.2.35 app/controllers/ecom/core/application_controller.rb
ecom_core-1.2.34 app/controllers/ecom/core/application_controller.rb
ecom_core-1.2.33 app/controllers/ecom/core/application_controller.rb
ecom_core-1.2.32 app/controllers/ecom/core/application_controller.rb