From 9d2f94955b18f9c2d382939245e027972c2aacb0 Mon Sep 17 00:00:00 2001 From: Greg Lazarev Date: Fri, 28 Jun 2013 14:08:04 -0700 Subject: [PATCH] Use cookies to store and lookup remember_token * reduces extra user lookups when adding cookie to headers * use the appropriate sign_out method in tests * resolves issue #295 --- lib/clearance/session.rb | 24 ++++++++++++------------ lib/clearance/testing/helpers.rb | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/clearance/session.rb b/lib/clearance/session.rb index 1e87a5089..bcdc46da1 100644 --- a/lib/clearance/session.rb +++ b/lib/clearance/session.rb @@ -7,16 +7,15 @@ def initialize(env) 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, - :secure => Clearance.configuration.secure_cookie, - :httponly => Clearance.configuration.httponly, - :path => '/' - ) - end + 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 @@ -27,6 +26,7 @@ def current_user def sign_in(user) @current_user = user + cookies[REMEMBER_TOKEN_COOKIE] = @current_user.remember_token end def sign_out @@ -57,8 +57,8 @@ def remember_token end def with_remember_token - if token = remember_token - yield token + if remember_token + yield remember_token end end end diff --git a/lib/clearance/testing/helpers.rb b/lib/clearance/testing/helpers.rb index a1aeeb2d4..2ac1c47cc 100644 --- a/lib/clearance/testing/helpers.rb +++ b/lib/clearance/testing/helpers.rb @@ -15,7 +15,7 @@ def sign_in_as(user) end def sign_out - @controller.current_user = nil + @controller.sign_out end end end