Sha256: e8a7e523004e0eb76533456e294bf372143cae364d0f6f33574bc4076c1ef6c1

Contents?: true

Size: 1.82 KB

Versions: 10

Compression:

Stored size: 1.82 KB

Contents

module Devise
  module Controllers
    # A module that may be optionally included in a controller in order
    # to provide remember me behavior.
    module Rememberable
      # Return default cookie values retrieved from session options.
      def self.cookie_values
        Rails.configuration.session_options.slice(:path, :domain, :secure)
      end

      # A small warden proxy so we can remember and forget uses from hooks.
      class Proxy #:nodoc:
        include Devise::Controllers::Rememberable

        delegate :cookies, :env, :to => :@warden

        def initialize(warden)
          @warden = warden
        end
      end

      # Remembers the given resource by setting up a cookie
      def remember_me(resource)
        return if env["devise.skip_storage"]
        scope = Devise::Mapping.find_scope!(resource)
        resource.remember_me!(resource.extend_remember_period)
        cookies.signed[remember_key(resource, scope)] = remember_cookie_values(resource)
      end

      # Forgets the given resource by deleting a cookie
      def forget_me(resource)
        scope = Devise::Mapping.find_scope!(resource)
        resource.forget_me!
        cookies.delete(remember_key(resource, scope), forget_cookie_values(resource))
      end

      protected

      def forget_cookie_values(resource)
        Devise::Controllers::Rememberable.cookie_values.merge!(resource.rememberable_options)
      end

      def remember_cookie_values(resource)
        options = { :httponly => true }
        options.merge!(forget_cookie_values(resource))
        options.merge!(
          :value => resource.class.serialize_into_cookie(resource),
          :expires => resource.remember_expires_at
        )
      end

      def remember_key(resource, scope)
        resource.rememberable_options.fetch(:key, "remember_#{scope}_token")
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
devise-3.1.2 lib/devise/controllers/rememberable.rb
devise-3.0.4 lib/devise/controllers/rememberable.rb
devise-2.2.8 lib/devise/controllers/rememberable.rb
devise-3.1.1 lib/devise/controllers/rememberable.rb
devise-3.1.0 lib/devise/controllers/rememberable.rb
devise-2.2.7 lib/devise/controllers/rememberable.rb
devise-3.0.3 lib/devise/controllers/rememberable.rb
devise-3.1.0.rc2 lib/devise/controllers/rememberable.rb
devise-2.2.6 lib/devise/controllers/rememberable.rb
devise-3.0.2 lib/devise/controllers/rememberable.rb