diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 888a921..9710ac4 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -2,5 +2,34 @@ module SessionsHelper def sign_in(user) cookies.permanent.signed[:remember_token] = [user.id, user.salt] + current_user = user end -end \ No newline at end of file + + def current_user=(user) + @current_user = user + end + + def current_user + @current_user ||= user_from_remember_token + end + + def signed_in? + !current_user.nil? + end + + def sign_out + cookies.delete(:remember_token) + self.current_user = nil + end + + private + + def user_from_remember_token + User.authenticate_with_salt(*remember_token) + end + + def remember_token + cookies.signed[:remember_token] || [nil, nil] + end +end + diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb index 4f40c4f..d1be04b 100644 --- a/spec/requests/users_spec.rb +++ b/spec/requests/users_spec.rb @@ -34,32 +34,32 @@ end.should change(User, :count).by(1) end end + end + + describe "signin" do - describe "signin" do - - describe "failure" do - it "should not sign a user in" do - visit signin_path - fill_in "Email", :with => "" - fill_in "Password", :with => "" - click_button - response.should have_selector('div.flash.error', - :content => "Invalid") - response.should render_template('sessions/new') - end + describe "failure" do + it "should not sign a user in" do + visit signin_path + fill_in "Email", :with => "" + fill_in "Password", :with => "" + click_button + response.should have_selector('div.flash.error', + :content => "Invalid") + response.should render_template('sessions/new') end - - describe "success" do - it "should sign a user in and out" do - user = Factory(:user) - visit signin_path - fill_in "Email", :with => user.email - fill_in "Password", :with => user.password - click_button - controller.should be_signed_in - click_link "Sign out" - controller.should_not be_signed_in - end + end + + describe "success" do + it "should sign a user in and out" do + user = Factory(:user) + visit signin_path + fill_in "Email", :with => user.email + fill_in "Password", :with => user.password + click_button + controller.should be_signed_in + click_link "Sign out" + controller.should_not be_signed_in end end end