From fa546e0fe5d120b6c5d55e718a58c362d80252b8 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 8 Nov 2024 10:19:13 -0800 Subject: [PATCH] Make rodauth and r.rodauth call default_rodauth_name for the default configuration to use This makes it easier to use multiple Rodauth configurations in an application. Previously, you had to specify the alternate configuration name in every case where you were calling the method. This allows you to set the configuration to use in a single place, so you can call rodauth and r.rodauth without arguments, and it will pick up the desired configuration. --- CHANGELOG | 2 + README.rdoc | 15 ++++++++ lib/rodauth.rb | 8 +++- spec/rodauth_spec.rb | 88 +++++++++++++++++++++++++------------------- 4 files changed, 73 insertions(+), 40 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ece4ac76..ad9332fe 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ === master +* Make rodauth and r.rodauth call default_rodauth_name for the default configuration to use (jeremyevans) + * Make clear_session not call the scope's clear session if using JWTs for session in the jwt feature (jeremyevans) * Support webauthn_autofill? configuration method in webauthn_autofill feature for disabiling autofill on login page (janko) (#445) diff --git a/README.rdoc b/README.rdoc index 4e901811..a17ac6c6 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1171,6 +1171,21 @@ the name as an argument to use that configuration: r.rodauth end +To prevent having to specify the name manually in all cases where you are +using it, you can define a +default_rodauth_name+ method in your Roda +application that returns the name that should be used: + + attr_reader :default_rodauth_name + + route do |r| + r.on 'secondary' do + @default_rodauth_name = :secondary + r.rodauth # will use the :secondary configuration + end + + r.rodauth # will use the default configuration + end + By default, alternate configurations will use the same session keys as the primary configuration, which may be undesirable. To ensure session state is separated between configurations, you can set a session key prefix for diff --git a/lib/rodauth.rb b/lib/rodauth.rb index d669d60d..fea7b663 100644 --- a/lib/rodauth.rb +++ b/lib/rodauth.rb @@ -402,7 +402,11 @@ def self.freeze end module InstanceMethods - def rodauth(name=nil) + def default_rodauth_name + nil + end + + def rodauth(name=default_rodauth_name) if name (@_rodauths ||= {})[name] ||= self.class.rodauth(name).new(self) else @@ -440,7 +444,7 @@ def freeze end module RequestMethods - def rodauth(name=nil) + def rodauth(name=scope.default_rodauth_name) scope.rodauth(name).route! end end diff --git a/spec/rodauth_spec.rb b/spec/rodauth_spec.rb index e94fd229..95ec0340 100644 --- a/spec/rodauth_spec.rb +++ b/spec/rodauth_spec.rb @@ -769,54 +769,66 @@ def foo fill_in 'Lonig', :with=>'foo@example.com' end - it "should support multiple rodauth configurations in an app" do - app = Class.new(Base) - app.plugin(:rodauth, rodauth_opts) do - enable :argon2 if RODAUTH_ALWAYS_ARGON2 - enable :login - if ENV['RODAUTH_SEPARATE_SCHEMA'] - password_hash_table Sequel[:rodauth_test_password][:account_password_hashes] - function_name do |name| - "rodauth_test_password.#{name}" + ["", " when using default_rodauth_name"].each do |type| + use_default_rodauth_name = type != "" + + it "should support multiple rodauth configurations in an app#{type}" do + app = Class.new(Base) + app.plugin(:rodauth, rodauth_opts) do + enable :argon2 if RODAUTH_ALWAYS_ARGON2 + enable :login + if ENV['RODAUTH_SEPARATE_SCHEMA'] + password_hash_table Sequel[:rodauth_test_password][:account_password_hashes] + function_name do |name| + "rodauth_test_password.#{name}" + end end end - end - app.plugin(:rodauth, rodauth_opts.merge(:name=>:r2)) do - enable :logout - end - - if Minitest::HooksSpec::USE_ROUTE_CSRF - app.plugin :route_csrf, Minitest::HooksSpec::ROUTE_CSRF_OPTS - end + app.plugin(:rodauth, rodauth_opts.merge(:name=>:r2)) do + enable :logout + end - app.route do |r| if Minitest::HooksSpec::USE_ROUTE_CSRF - check_csrf! + app.plugin :route_csrf, Minitest::HooksSpec::ROUTE_CSRF_OPTS end - r.on 'r1' do - r.rodauth - 'r1' + + if use_default_rodauth_name + app.define_method(:default_rodauth_name){request.path.start_with?('/r2') ? :r2 : nil} end - r.on 'r2' do - r.rodauth(:r2) - 'r2' + + app.route do |r| + if Minitest::HooksSpec::USE_ROUTE_CSRF + check_csrf! + end + r.on 'r1' do + r.rodauth + 'r1' + end + r.on 'r2' do + if use_default_rodauth_name + r.rodauth + else + r.rodauth(:r2) + end + 'r2' + end + rodauth.session_value.inspect end - rodauth.session_value.inspect - end - app.freeze - self.app = app + app.freeze + self.app = app - login(:path=>'/r1/login') - page.body.must_equal DB[:accounts].get(:id).inspect + login(:path=>'/r1/login') + page.body.must_equal DB[:accounts].get(:id).inspect - visit '/r2/logout' - click_button 'Logout' - page.body.must_equal 'nil' + visit '/r2/logout' + click_button 'Logout' + page.body.must_equal 'nil' - visit '/r1/logout' - page.body.must_equal 'r1' - visit '/r2/login' - page.body.must_equal 'r2' + visit '/r1/logout' + page.body.must_equal 'r1' + visit '/r2/login' + page.body.must_equal 'r2' + end end it "should support account_select setting for choosing account columns" do