Skip to content

Commit

Permalink
Make rodauth and r.rodauth call default_rodauth_name for the default …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
jeremyevans committed Nov 8, 2024
1 parent b6f368b commit fa546e0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
15 changes: 15 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/rodauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
88 changes: 50 additions & 38 deletions spec/rodauth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fa546e0

Please sign in to comment.