Module: Authstrategies::Manager

Included in:
Middleware
Defined in:
lib/authstrategies.rb

Constant Summary collapse

@@callbacks =
{}
@@config =
{
  :default_locales => :en,
   :after_login_path => '/',
  :after_login_failure_path => '/login',
  :after_logout_path => '/',
  :after_signup_path => '/',
}

Class Method Summary collapse

Class Method Details

.after_authentication(&block) ⇒ Object

Executed every time the user is authenticated (first time in each session). Courtesy of Warden, for more information check the warden callbacks wiki



71
72
73
74
75
# File 'lib/authstrategies.rb', line 71

def self.after_authentication &block
  Warden::Manager.after_authentication do |user, auth, opts|
    yield(user, auth, opts)
  end
end

.after_login(&block) ⇒ Object

This is called each time after the user logs in 3 parameters are passed to this callback

>current_user - the user that hase just been set

>request - the request data

>response - the response data



118
119
120
# File 'lib/authstrategies.rb', line 118

def self. &block
  self.register :after_login, &block
end

.after_login_failure(&block) ⇒ Object

This is called in the failure application Useful for redirecting the user after he logs in 2 params are passed to this callback

>request - the request data

>response - the response data



91
92
93
# File 'lib/authstrategies.rb', line 91

def self. &block
  self.register :after_login_failure, &block
end

.after_logout(&block) ⇒ Object

This is called after the user is logged out. Useful for redirecting the user after logging out 2 parameters are passed to this callback

>request - the request data

>response - the response data



109
110
111
# File 'lib/authstrategies.rb', line 109

def self.after_logout &block
  self.register :after_logout, &block
end

.after_set_user(&block) ⇒ Object

This is called every time the user is set. The user is set:

> on each request when they are accessed for the first time via env.user

> when the user is initially authenticated

> when the user is set via the set_user method

Courtesy of Warden, for more information check the warden callbacks wiki



61
62
63
64
65
# File 'lib/authstrategies.rb', line 61

def self.after_set_user &block
  Warden::Manager.after_set_user do |user, auth, opts|
    yield(user, auth, opts)
  end
end

.after_signup(&block) ⇒ Object

This is called after the user is saved into the database 3 parameters are passed to this callback

>user - the user that just signed up

>request - the request data

>response - the response data

Also since the user is set to session via env.set_user after_set_user is also called after the user signs up



130
131
132
# File 'lib/authstrategies.rb', line 130

def self. &block
  self.register :after_signup, &block
end

.before_login_failure(&block) ⇒ Object

This callback is run right before the failure application is called. Courtesy of Warden, for more information check the warden callbacks wiki



80
81
82
83
84
# File 'lib/authstrategies.rb', line 80

def self. &block
  Warden::Manager.before_failure do |env, opts|
    yield(env, opts)
  end
end

.before_logout(&block) ⇒ Object

This callback is run before each user is logged out. Courtesy of Warden, for more information check the warden callbacks wiki



98
99
100
101
102
# File 'lib/authstrategies.rb', line 98

def self.before_logout &block
  Warden::Manager.before_logout do |user, auth, opts|
    yield(user, auth, opts)
  end
end

.call(hook, args = []) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/authstrategies.rb', line 42

def self.call hook, args = []
  if self.registered? hook
    @@callbacks[hook].each do |callback|
      callback.call(args)
    end
  end
end

.config {|@@config| ... } ⇒ Object

Yields:



50
51
52
53
# File 'lib/authstrategies.rb', line 50

def self.config &block
  yield(@@config) if block_given?
  @@config
end

.register(hook, &block) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/authstrategies.rb', line 34

def self.register hook, &block
  if self.registered? hook
    @@callbacks[hook].push block
  else
    @@callbacks[hook] = [block]
  end
end

.registered?(hook) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/authstrategies.rb', line 30

def self.registered? hook
  @@callbacks.has_key? hook
end