Method: Warden::Hooks#after_set_user

Defined in:
lib/warden/hooks.rb

#after_set_user(options = {}, method = :push, &block) ⇒ Object

A callback hook set to run every time after a user is set. This callback is triggered the first time one of those three events happens during a request: :authentication, :fetch (from session) and :set_user (when manually set). You can supply as many hooks as you like, and they will be run in order of declaration.

If you want to run the callbacks for a given scope and/or event, you can specify them as options. See parameters and example below.

Parameters: <options> Some options which specify when the callback should be executed

scope  - Executes the callback only if it matches the scope(s) given
only   - Executes the callback only if it matches the event(s) given
except - Executes the callback except if it matches the event(s) given

<block> A block where you can set arbitrary logic to run every time a user is set

Block Parameters: |user, auth, opts|
  user - The user object that is being set
  auth - The raw authentication proxy object.
  opts - any options passed into the set_user call including :scope

Example:

Warden::Manager.after_set_user do |user,auth,opts|
  scope = opts[:scope]
  if auth.session["#{scope}.last_access"].to_i > (Time.now - 5.minutes)
    auth.logout(scope)
    throw(:warden, :scope => scope, :reason => "Times Up")
  end
  auth.session["#{scope}.last_access"] = Time.now
end

Warden::Manager.after_set_user :except => :fetch do |user,auth,opts|
  user. += 1
end

:api: public

Raises:

  • (BlockNotGiven)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/warden/hooks.rb', line 53

def after_set_user(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?

  if options.key?(:only)
    options[:event] = options.delete(:only)
  elsif options.key?(:except)
    options[:event] = [:set_user, :authentication, :fetch] - Array(options.delete(:except))
  end

  _after_set_user.send(method, [block, options])
end