Sha256: 0003c234e42bf7b9e6515b4cca4f517072f3d9483b970687847d74f07bef2d83

Contents?: true

Size: 1.91 KB

Versions: 22

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

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

      def remember_me_is_active?(resource)
        return false unless resource.respond_to?(:remember_me)
        scope = Devise::Mapping.find_scope!(resource)
        _, token, generated_at = cookies.signed[remember_key(resource, scope)]
        resource.remember_me?(token, generated_at)
      end

      # Remembers the given resource by setting up a cookie
      def remember_me(resource)
        return if request.env["devise.skip_storage"]
        scope = Devise::Mapping.find_scope!(resource)
        resource.remember_me!
        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

22 entries across 22 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/devise-4.9.4/lib/devise/controllers/rememberable.rb
devise-4.9.4 lib/devise/controllers/rememberable.rb
devise-4.9.3 lib/devise/controllers/rememberable.rb
devise-4.9.2 lib/devise/controllers/rememberable.rb
devise-4.9.1 lib/devise/controllers/rememberable.rb
devise-4.9.0 lib/devise/controllers/rememberable.rb
devise-4.8.1 lib/devise/controllers/rememberable.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/devise-4.8.0/lib/devise/controllers/rememberable.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/devise-4.8.0/lib/devise/controllers/rememberable.rb
devise-4.8.0 lib/devise/controllers/rememberable.rb
devise-4.7.3 lib/devise/controllers/rememberable.rb
devise-4.7.2 lib/devise/controllers/rememberable.rb
devise-4.7.1 lib/devise/controllers/rememberable.rb
devise-4.7.0 lib/devise/controllers/rememberable.rb
devise-4.6.2 lib/devise/controllers/rememberable.rb
devise-4.6.1 lib/devise/controllers/rememberable.rb
devise-4.6.0 lib/devise/controllers/rememberable.rb
devise-4.5.0 lib/devise/controllers/rememberable.rb
devise-4.4.3 lib/devise/controllers/rememberable.rb
devise-4.4.2 lib/devise/controllers/rememberable.rb