Sha256: 9d124ffd5bd8032799bcc323d3e9cd401f0a5632d139cccaf52ca4d58a8cf673

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Clearance
  class Session
    REMEMBER_TOKEN_COOKIE = 'remember_token'.freeze

    def initialize(env)
      @env = env
    end

    def add_cookie_to_headers(headers)
      Rack::Utils.set_cookie_header!(
        headers,
        REMEMBER_TOKEN_COOKIE,
        :value => remember_token,
        :expires => Clearance.configuration.cookie_expiration.call,
        :secure => Clearance.configuration.secure_cookie,
        :httponly => Clearance.configuration.httponly,
        :path => '/'
      )
    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
      cookies[REMEMBER_TOKEN_COOKIE] = @current_user.remember_token
    end

    def sign_out
      if signed_in?
        current_user.reset_remember_token!
      end

      @current_user = nil
      cookies.delete REMEMBER_TOKEN_COOKIE
    end

    def signed_in?
      current_user.present?
    end

    def signed_out?
      ! signed_in?
    end

    private

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

    def remember_token
      cookies[REMEMBER_TOKEN_COOKIE]
    end

    def with_remember_token
      if remember_token
        yield remember_token
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clearance-1.0.0 lib/clearance/session.rb
clearance-1.0.0.rc8 lib/clearance/session.rb