public
Description: A simple model based ruby authentication solution.
Homepage: http://rdoc.info/projects/binarylogic/authlogic
Clone URL: git://github.com/binarylogic/authlogic.git
Click here to lend your support to: authlogic and make a donation at www.pledgie.com !
authlogic / lib / authlogic / session / cookies.rb
100644 40 lines (36 sloc) 1.18 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Authlogic
  module Session
    # = Cookies
    #
    # Handles all authentication that deals with cookies, such as persisting a session and saving / destroying a session.
    module Cookies
      def self.included(klass)
        klass.after_save :save_cookie, :if => :persisting?
        klass.after_destroy :destroy_cookie, :if => :persisting?
      end
      
      # Tries to validate the session from information in the cookie
      def valid_cookie?
        if cookie_credentials
          self.unauthorized_record = search_for_record("find_by_#{persistence_token_field}", cookie_credentials)
          valid?
        else
          false
        end
      end
      
      private
        def cookie_credentials
          controller.cookies[cookie_key]
        end
        
        def save_cookie
          controller.cookies[cookie_key] = {
            :value => record.send(persistence_token_field),
            :expires => remember_me_until,
            :domain => controller.cookie_domain
          }
        end
        
        def destroy_cookie
          controller.cookies.delete cookie_key, :domain => controller.cookie_domain
        end
    end
  end
end