Sha256: 734d405b173bc9108ef0c86886a09819795ba932e724ba05d4b5d3af7a2a243c

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Auth0CurrentUser
  module WebSecured
    extend ActiveSupport::Concern

    included do
      helper_method :current_user
      before_action :logged_in_using_omniauth?
    end

    def current_user
      @_current_user ||= RequestStore.store[:current_user] ||= Kernel.const_get(authenticated_klass).find_by(email: email) 
    end

    private

    def authenticated_klass
      unless configuration.authenticated_klass
        raise NotImplementedError, 'You must define the #authenitcated_klass in config/initializers/auth0_current_user'
        return
      end

      @authenticated_klass ||= configuration.authenticated_klass.to_s.classify
    rescue NameError => e
      Rails.logger.error("You must create a #{authenticated_klass} model/migration")
    rescue StandardError => e
      Rails.logger.error(e.message)
    end

    def configuration
      @configuration ||= Configuration.new
    end

    def email
      @_email ||= session.dig(:userinfo, :email)
    end

    def logged_in_using_omniauth?
      redirect_to '/' unless current_user || session[:userinfo].present?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
auth0_current_user-0.2.0 lib/auth0_current_user/web_secured.rb