Sha256: d78e2460ff59eb08fb0f72370552ecd8bf0a3f86eae454bf0e414f99580a05bd

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

require 'mks/auth/token_auth'
require 'mks/common/methodresponse'

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

6 entries across 6 versions & 1 rubygems

Version Path
mks_auth-1.0.13 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.12 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.11 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.10 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.9 app/controllers/mks/auth/application_controller.rb
mks_auth-1.0.8 app/controllers/mks/auth/application_controller.rb