Provides guest user functionality for Rodauth.
Add the gem to your project:
$ bundle add rodauth-guestStart by enabling the guest feature in your Rodauth configuration:
plugin :rodauth do
enable :guest
endThe feature provides the ability to automatically create and log in anonymous accounts when no account is not logged in. You just need to choose for which routes you want to allow guest users:
route do |r|
r.rodauth # route rodauth requests
if r.path.start_with?("/dashboard")
rodauth.allow_guest # automatically creates a guest account
end
endThe guest account will be logged in the same way normal accounts are, so logged_in? & authenticated? will return true, and session_value will return the guest account ID.
If you want to check whether the logged in account is a guest account:
if rodauth.guest_logged_in?
# ...
endGuest accounts are unverified by default, and have a random email address in the form of guest_<RANDOM_STRING>@example.com.
rodauth.account_from_session
rodauth.account #=> { id: 1, status_id: 1, email: "[email protected]" }You can set additional column data on guest account creation:
# in a schema migration:
add_column :accounts, :guest, :boolean, default: truebefore_create_guest do
account[:guest] = true
endTo override new email generation:
new_guest_login { "guest_#{SecureRandom.hex}@example.com" }Or just the unique suffix:
guest_unique_suffix { SecureRandom.hex }The logged in guest account is automatically deleted when a new account is created. You can use a before hook to transfer any data from the guest account into the new user:
before_delete_guest do
# session_value - guest account ID
# account_id - new account ID
db[:articles].where(account_id: session_value).update(account_id: account_id)
endYou can also skip deletion of the guest record on account creation (by default it's skipped when create_account_autologin? is false):
delete_guest_on_create? falseRun tests with Rake:
$ bundle exec rake testThe gem is available as open source under the terms of the MIT License.
Everyone interacting in the rodauth-guest project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
This gem was inspired by devise-guests.