janko / rodauth-rails

Rails integration for Rodauth authentication framework
https://github.com/jeremyevans/rodauth
MIT License
584 stars 40 forks source link

Handling Admin User #23

Closed j-manu closed 3 years ago

j-manu commented 3 years ago

In devise when I want to have admin users, I create a separate model AdminUser. How do I handle that here?

janko commented 3 years ago

Assuming that you want special authentication behaviour for admin users, you would typically create a separate Rodauth configuration with a diffferent route prefix:

class RodauthApp < Rodauth::Rails::App
  # main users
  configure do
    # ...
  end

  # admin users
  configure(:admin) do
    enable ... # admin features
    prefix "/admin"
  end

  route do |r|
    # ...
    r.rodauth
    r.on("admin") { r.rodauth(:admin) } # route admin authentication requests
    # ...
  end
end

You should be able to reuse the main accounts table and other account_* tables for storing admin users. However, if you need things like separate email uniqueness validation, you can choose a different table:

configure(:admin) do
  # ...
  accounts_table :admin_accounts
  password_hash_table :admin_account_password_hashes
  # ... and so on ...
end