Skip to content

Commit fa546e0

Browse files
committed
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.
1 parent b6f368b commit fa546e0

File tree

4 files changed

+73
-40
lines changed

4 files changed

+73
-40
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Make rodauth and r.rodauth call default_rodauth_name for the default configuration to use (jeremyevans)
4+
35
* Make clear_session not call the scope's clear session if using JWTs for session in the jwt feature (jeremyevans)
46

57
* Support webauthn_autofill? configuration method in webauthn_autofill feature for disabiling autofill on login page (janko) (#445)

README.rdoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,21 @@ the name as an argument to use that configuration:
11711171
r.rodauth
11721172
end
11731173

1174+
To prevent having to specify the name manually in all cases where you are
1175+
using it, you can define a +default_rodauth_name+ method in your Roda
1176+
application that returns the name that should be used:
1177+
1178+
attr_reader :default_rodauth_name
1179+
1180+
route do |r|
1181+
r.on 'secondary' do
1182+
@default_rodauth_name = :secondary
1183+
r.rodauth # will use the :secondary configuration
1184+
end
1185+
1186+
r.rodauth # will use the default configuration
1187+
end
1188+
11741189
By default, alternate configurations will use the same session keys as the
11751190
primary configuration, which may be undesirable. To ensure session state is
11761191
separated between configurations, you can set a session key prefix for

lib/rodauth.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ def self.freeze
402402
end
403403

404404
module InstanceMethods
405-
def rodauth(name=nil)
405+
def default_rodauth_name
406+
nil
407+
end
408+
409+
def rodauth(name=default_rodauth_name)
406410
if name
407411
(@_rodauths ||= {})[name] ||= self.class.rodauth(name).new(self)
408412
else
@@ -440,7 +444,7 @@ def freeze
440444
end
441445

442446
module RequestMethods
443-
def rodauth(name=nil)
447+
def rodauth(name=scope.default_rodauth_name)
444448
scope.rodauth(name).route!
445449
end
446450
end

spec/rodauth_spec.rb

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -769,54 +769,66 @@ def foo
769769
fill_in 'Lonig', :with=>'foo@example.com'
770770
end
771771

772-
it "should support multiple rodauth configurations in an app" do
773-
app = Class.new(Base)
774-
app.plugin(:rodauth, rodauth_opts) do
775-
enable :argon2 if RODAUTH_ALWAYS_ARGON2
776-
enable :login
777-
if ENV['RODAUTH_SEPARATE_SCHEMA']
778-
password_hash_table Sequel[:rodauth_test_password][:account_password_hashes]
779-
function_name do |name|
780-
"rodauth_test_password.#{name}"
772+
["", " when using default_rodauth_name"].each do |type|
773+
use_default_rodauth_name = type != ""
774+
775+
it "should support multiple rodauth configurations in an app#{type}" do
776+
app = Class.new(Base)
777+
app.plugin(:rodauth, rodauth_opts) do
778+
enable :argon2 if RODAUTH_ALWAYS_ARGON2
779+
enable :login
780+
if ENV['RODAUTH_SEPARATE_SCHEMA']
781+
password_hash_table Sequel[:rodauth_test_password][:account_password_hashes]
782+
function_name do |name|
783+
"rodauth_test_password.#{name}"
784+
end
781785
end
782786
end
783-
end
784-
app.plugin(:rodauth, rodauth_opts.merge(:name=>:r2)) do
785-
enable :logout
786-
end
787-
788-
if Minitest::HooksSpec::USE_ROUTE_CSRF
789-
app.plugin :route_csrf, Minitest::HooksSpec::ROUTE_CSRF_OPTS
790-
end
787+
app.plugin(:rodauth, rodauth_opts.merge(:name=>:r2)) do
788+
enable :logout
789+
end
791790

792-
app.route do |r|
793791
if Minitest::HooksSpec::USE_ROUTE_CSRF
794-
check_csrf!
792+
app.plugin :route_csrf, Minitest::HooksSpec::ROUTE_CSRF_OPTS
795793
end
796-
r.on 'r1' do
797-
r.rodauth
798-
'r1'
794+
795+
if use_default_rodauth_name
796+
app.define_method(:default_rodauth_name){request.path.start_with?('/r2') ? :r2 : nil}
799797
end
800-
r.on 'r2' do
801-
r.rodauth(:r2)
802-
'r2'
798+
799+
app.route do |r|
800+
if Minitest::HooksSpec::USE_ROUTE_CSRF
801+
check_csrf!
802+
end
803+
r.on 'r1' do
804+
r.rodauth
805+
'r1'
806+
end
807+
r.on 'r2' do
808+
if use_default_rodauth_name
809+
r.rodauth
810+
else
811+
r.rodauth(:r2)
812+
end
813+
'r2'
814+
end
815+
rodauth.session_value.inspect
803816
end
804-
rodauth.session_value.inspect
805-
end
806-
app.freeze
807-
self.app = app
817+
app.freeze
818+
self.app = app
808819

809-
login(:path=>'/r1/login')
810-
page.body.must_equal DB[:accounts].get(:id).inspect
820+
login(:path=>'/r1/login')
821+
page.body.must_equal DB[:accounts].get(:id).inspect
811822

812-
visit '/r2/logout'
813-
click_button 'Logout'
814-
page.body.must_equal 'nil'
823+
visit '/r2/logout'
824+
click_button 'Logout'
825+
page.body.must_equal 'nil'
815826

816-
visit '/r1/logout'
817-
page.body.must_equal 'r1'
818-
visit '/r2/login'
819-
page.body.must_equal 'r2'
827+
visit '/r1/logout'
828+
page.body.must_equal 'r1'
829+
visit '/r2/login'
830+
page.body.must_equal 'r2'
831+
end
820832
end
821833

822834
it "should support account_select setting for choosing account columns" do

0 commit comments

Comments
 (0)