]> git.openstreetmap.org Git - rails.git/commitdiff
Make the "remember me" option work as intended
authorTom Hughes <tom@compton.nu>
Tue, 5 Nov 2024 18:42:20 +0000 (18:42 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 13 Nov 2024 12:18:23 +0000 (12:18 +0000)
app/controllers/sessions_controller.rb
app/views/sessions/new.html.erb
test/controllers/sessions_controller_test.rb

index a3e6f42f03db4b172607bc26285d6c79c3b0ee8b..abbaf5e921e45aedf89e60d7057023b9a0374143 100644 (file)
@@ -20,7 +20,7 @@ class SessionsController < ApplicationController
   end
 
   def create
-    session[:remember_me] ||= params[:remember_me]
+    session[:remember_me] = params[:remember_me] == "yes"
 
     referer = safe_referer(params[:referer]) if params[:referer]
 
index 9d05d4af80d44b7bf956824ee163dc66d4db225f..c2d96b63cc37ae2b9def6ba868cf8742927c3212 100644 (file)
@@ -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 %>
 
   <div class="mb-3">
index 914a4ab5608d32ce7fe343a2d6d5556ba47d3f31..f490b748c4a6c763699d2759909ba06f52944e69 100644 (file)
@@ -54,6 +54,24 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest
     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