Skip to content

Commit

Permalink
Use cookies to store and lookup remember_token
Browse files Browse the repository at this point in the history
* reduces extra user lookups when adding cookie to headers
* use the appropriate sign_out method in tests
* resolves issue #295
  • Loading branch information
Greg Lazarev committed Jul 15, 2013
1 parent 3a06122 commit 9d2f949
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions lib/clearance/session.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/clearance/testing/helpers.rb
Expand Up @@ -15,7 +15,7 @@ def sign_in_as(user)
end

def sign_out
@controller.current_user = nil
@controller.sign_out
end
end
end
Expand Down

0 comments on commit 9d2f949

Please sign in to comment.