Sha256: 21a1b2ec9075c0deb9b646be52400f11effff9e77103fd5d8d40b3c1b0860577

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Janus
  module Hooks # :nodoc:
    extend ActiveSupport::Concern

    # Hooks allow you the react at the different steps of a user session.
    # All callbacks will receive the same arguments: +user+, +manager+ and
    # +options+.
    # 
    # Example:
    # 
    #   Janus::Manager.after_login do |user, manager, options|
    #     session = manager.session(options[:scope])
    #     
    #     # write some great code here
    #   end
    # 
    # Options:
    # 
    # - +:scope+
    # 
    module ClassMethods
      # Executed after a strategy succeeds to authenticate a user.
      def after_authenticate(&block)
        add_callback(:authenticate, block)
      end

      # Executed the first time an authenticated user is fetched from session.
      def after_fetch(&block)
        add_callback(:fetch, block)
      end

      # Executed after a user is logged in.
      def after_login(&block)
        add_callback(:login, block)
      end

      # Executed after a user is logged out. after_logout will be executed for
      # each scope when logging out from multiple scopes at once.
      def after_logout(&block)
        add_callback(:logout, block)
      end

      def run_callbacks(kind, *args) # :nodoc:
        callbacks(kind).each { |block| block.call(*args) }
      end

      private
        def add_callback(kind, block)
          callbacks(kind) << block
        end

        def callbacks(kind)
          @callbacks ||= {}
          @callbacks[kind] ||= []
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
janus-0.6.0 lib/janus/hooks.rb
janus-0.5.0 lib/janus/hooks.rb