Sha256: 2927d158e8025ef011481d56f0795848ce483ea3bbbc6a663e9525ba60bc3d6d

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Clearance
  class Session
    REMEMBER_TOKEN_COOKIE = "remember_token".freeze

    def initialize(env)
      @env = env
    end

    def signed_in?
      current_user.present?
    end

    def current_user
      @current_user ||= with_remember_token do |token|
        Clearance.configuration.user_model.find_by_remember_token(token)
      end
    end

    def sign_in(user)
      @current_user = user
    end

    def sign_out
      current_user.reset_remember_token! if signed_in?
      @current_user = nil
      cookies.delete(REMEMBER_TOKEN_COOKIE)
    end

    def add_cookie_to_headers(headers)
      if signed_in?
        Rack::Utils.set_cookie_header!(headers,
                                       REMEMBER_TOKEN_COOKIE,
                                       :value => current_user.remember_token,
                                       :expires => Clearance.configuration.cookie_expiration.call,
                                       :path => "/")
      end
    end

    private

    def with_remember_token
      if token = remember_token
        yield token
      end
    end

    def remember_token
      cookies[REMEMBER_TOKEN_COOKIE]
    end

    def cookies
      @cookies ||= @env['action_dispatch.cookies'] || Rack::Request.new(@env).cookies
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clearance-0.16.3 lib/clearance/session.rb
clearance-0.16.2 lib/clearance/session.rb
clearance-0.16.1 lib/clearance/session.rb