Sha256: cb6d8976d68cf4822dce7e443932fb2538b7c1ce83a07f81f72767c4d4de2969

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

require 'mks/auth/token_auth'

module Mks
  module Auth
    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

      def current_user_id
        auth['id']
      end

      def app_code
        Rails.application.config.app_code
      end

      private

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

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

      def auth
        TokenAuth.decode(token)
      end

      def auth_present?
        bearer = request.env.fetch('HTTP_AUTHORIZATION', '').scan(/Bearer/).flatten.first
        !bearer.nil?
      end

      def app_module
        code = Rails.configuration.app_code
        ApplicationModule.find_by(code: code)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mks_auth-1.0.14 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.7 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.6 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.5 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.4 app/controllers/mks/auth/application_controller.rb