Sha256: fae60d6ea4fc3e972c9148221070ec467d4c7889e7ea3e9a0d436cf068886c62

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module CurrentSession
  #
  # Interface for manipulating http request objects
  #
  module Interface
    extend ActiveSupport::Concern
    class_methods do
      def call(request)
        update(request).current_user.present?
      end

      def update(request)
        session_repository(request).try do |repository|
          repository.find { |user| self.current_user = user }
        end
        self
      end

      def create(request)
        auth = @auth_class.new(request, user_class)
        auth.find_or_create_by_auth.try do |user|
          auth.update(user)
          session_repository(request).update_session_token(user)
        end
      end

      def destroy(request)
        self.current_user = nil
        session_repository(request).try do |repository|
          repository.destroy
          repository.delete_session_token
        end
      end

      private

      def session_repository(request)
        @session_repository_class.new(
          request: request,
          user_class: user_class,
          session_token_class: session_token_class,
          current_time: current_time(request)
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
current_session-0.1.3 lib/current_session/interface.rb
current_session-0.1.2 lib/current_session/interface.rb
current_session-0.1.0 lib/current_session/interface.rb