From a42b6546060227fce391c54557d7383f0291f410 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 5 Nov 2024 18:42:20 +0000 Subject: [PATCH] Make the "remember me" option work as intended --- app/controllers/sessions_controller.rb | 2 +- app/views/sessions/new.html.erb | 2 +- test/controllers/sessions_controller_test.rb | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index a3e6f42f03..abbaf5e921 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -20,7 +20,7 @@ def new end def create - session[:remember_me] ||= params[:remember_me] + session[:remember_me] = params[:remember_me] == "yes" referer = safe_referer(params[:referer]) if params[:referer] diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index 9d05d4af80..c2d96b63cc 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -40,7 +40,7 @@ <%= f.password_field :password, :autocomplete => "on", :tabindex => 2, :value => "", :skip_label => true %> <%= f.form_group do %> - <%= f.check_box :remember_me, { :label => t(".remember"), :tabindex => 3, :checked => (params[:remember_me] == "yes") }, "yes" %> + <%= f.check_box :remember_me, { :label => t(".remember"), :tabindex => 3, :checked => (params[:remember_me] == "true") }, "yes" %> <% end %>
diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index 914a4ab560..f490b748c4 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -54,6 +54,24 @@ def test_login assert_redirected_to root_path end + def test_login_remembered + user = create(:user) + + post login_path, :params => { :username => user.display_name, :password => "test", :remember_me => "yes" } + assert_redirected_to root_path + + assert_equal 28 * 86400, session[:_remember_for] + end + + def test_login_not_remembered + user = create(:user) + + post login_path, :params => { :username => user.display_name, :password => "test", :remember_me => "0" } + assert_redirected_to root_path + + assert_nil session[:_remember_for] + end + def test_logout_without_referer post logout_path assert_redirected_to root_path