Sha256: 7ccc2f4f40069a4b45967deda9f777fb30bd82e7e04bd4fae48fbe74f642ebb5

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module RememberMe
  module Controller
    def cookie_values
      Rails.configuration.session_options.slice(:path, :domain, :secure)
    end

    def remember_me?
      params.has_key?(:remember_me) && params[:remember_me]
    end

    def scope_of(resource)
      resource.class.to_s.underscore
    end

    def remember_me(resource)
      resource.remember_me!
      cookies.signed[remember_key(scope_of resource)] = remember_cookie_values(resource)
    end

    def forget_me(resource)
      resource.forget_me!
      cookies.delete remember_key(scope_of resource), forget_cookie_values(resource)
    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 forget_cookie_values(resource)
      cookie_values.merge!(resource.rememberable_options)
    end

    def remember_key(scope)
      # remember_token_for_user
      :"_rtf#{scope}"
    end

    def remember(scope)
      cookie = cookies.signed[remember_key(scope)]
      resource = cookie ?
        "#{scope.classify}".constantize.serialize_from_cookie(*cookie) : nil
      if block_given?
        yield resource
      else
        resource
      end
    rescue
      # see: https://github.com/linyows/remember_me/pull/1
      # `Moped` NameError occured when upgrade mongoid v4 to v5
      nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remember_me-1.0.0 lib/remember_me/controller.rb