Skip to content
Janko Marohnić edited this page Sep 29, 2022 · 15 revisions

When using multiple Rodauth configurations, you'll likely want to save the information of which account belongs to which configuration to the database. One way would be to have a separate table that stores account types:

$ rails generate migration create_account_types
# db/migrate/*_create_account_types.rb
class CreateAccountTypes < ActiveRecord::Migration
  def change
    create_table :account_types do |t|
      t.references :account, foreign_key: { on_delete: :cascade }, null: false
      t.string :type, null: false
    end
  end
end
$ rails db:migrate

Then an entry would be inserted after account creation, and optionally whenever Rodauth retrieves accounts you could filter only those belonging to the current configuration:

# app/misc/rodauth_admin.rb
class RodauthAdmin < Rodauth::Rails::Auth
  configure do
    # ...
    after_create_account do
      db[:account_types].insert(account_id: account_id, type: "admin")
    end
    auth_class_eval do
      def account_ds(*)
        super.where(id: db[:account_types].where(type: "admin").select(:account_id))
      end
    end
    # ...
  end
end
Clone this wiki locally