Module: Auth::Behavior::RememberMe::ControllerExtensions

Defined in:
lib/auth/behavior/remember_me/controller_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/auth/behavior/remember_me/controller_extensions.rb', line 52

def self.included(base)
  base.class_eval do
    hide_action :login_with_remembrance!, :login_without_remembrance!, :authenticate_current_user_without_remembrance,
                :authenticate_current_user_with_remembrance, :authenticate_with_remembrance_token,
                :set_remembrance_token_cookie, :handle_remembrance_token_theft, :logout_with_remembrance!,
                :logout_without_remembrance!
    
    unless method_defined?(:login_without_remembrance!)
      alias_method_chain :login!, :remembrance
      alias_method_chain :logout!, :remembrance
      alias_method_chain :authenticate_current_user, :remembrance
    end
  end
end

Instance Method Details

#authenticate_current_user_with_remembranceObject



20
21
22
23
24
25
# File 'lib/auth/behavior/remember_me/controller_extensions.rb', line 20

def authenticate_current_user_with_remembrance
  authenticate_current_user_without_remembrance
  if @current_user == false
    authenticate_with_remembrance_token
  end
end

#authenticate_with_remembrance_tokenObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/auth/behavior/remember_me/controller_extensions.rb', line 27

def authenticate_with_remembrance_token
  if token_value = cookies[:remembrance_token]
    token = RemembranceToken.find_by_value(token_value)
    if token
      @current_user = token.authenticatable
      handle_remembrance_token_theft(token) if token.theft?
      token.regenerate
      token.save
      set_remembrance_token_cookie(token)
    end
  end
end

#handle_remembrance_token_theft(token) ⇒ Object



40
41
42
43
# File 'lib/auth/behavior/remember_me/controller_extensions.rb', line 40

def handle_remembrance_token_theft(token)
  flash[:error] = Auth.remember_me.token_theft_message
  token.authenticatable.remembrance_tokens.destroy_all
end

#login_with_remembrance!(user, options = {}) ⇒ Object

If a :remember option is given, a remembrance cookie will be set. If omitted, the cookie will be, too.



3
4
5
6
7
8
9
10
# File 'lib/auth/behavior/remember_me/controller_extensions.rb', line 3

def (user, options = {})
  (user)
  
  if options[:remember]
    token = RemembranceToken.create!(:authenticatable => user)
    set_remembrance_token_cookie(token)
  end
end

#logout_with_remembrance!(options = {}) ⇒ Object

If a :forget option is given, the remembrance cookie will also be deleted.



13
14
15
16
17
18
# File 'lib/auth/behavior/remember_me/controller_extensions.rb', line 13

def logout_with_remembrance!(options = {})
  logout_without_remembrance!
  if options[:forget]
    cookies.delete(:remembrance_token)
  end
end


45
46
47
48
49
50
# File 'lib/auth/behavior/remember_me/controller_extensions.rb', line 45

def set_remembrance_token_cookie(token)
  cookies[:remembrance_token] = {
    :value => token.value,
    :expires => Auth.remember_me.duration.from_now
  }
end