module ControllerAfterAuthenticationHooks
  extend ActiveSupport::Concern

  included do
    class_attribute :after_authentication_hooks
  end

  def run_after_authentication_hooks!
    Array.wrap(after_authentication_hooks).each do |hook|
      next if hook[:options][:only].present? && !Array.wrap(hook[:options][:only]).include?(action_name.to_sym)
      instance_eval &hook[:block]
    end
  end

  module ClassMethods
    def after_authentication(options, &blk)
      self.after_authentication_hooks ||= []
      after_authentication_hooks << {options: options, block: blk}
    end
  end
end
