Devise
Devise is fully supported but entirely optional. The engine works without any authentication system.
When Devise is installed
The engine automatically detects Devise and provides:
- Styled sign in, registration, and password reset, confirmation, and unlock views
- Header links that adapt to authentication state
- User info display in sidebar when signed in
Installation
Add Devise to your application:
bundle add devise
Run the Devise installer and generate a User model:
bin/rails generate devise:install
bin/rails generate devise User
bin/rails db:migrate
Add Devise routes to your routes.rb:
devise_for :users
Configuration
The engine uses current_user by default. If your app uses a different method, configure it in an initializer:
Layered::Ui.current_user_method = :current_admin
The sidebar displays the signed-in user's email. If your user model has a name attribute, it will be displayed as well. To support this, add a name column to your users migration:
add_column :users, :name, :string
Then permit the name parameter in your ApplicationController:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
Helper methods
These helpers allow your views to work with or without Devise:
| Helper | Description |
|---|---|
l_ui_devise_installed? |
Returns true if Devise is available |
l_ui_user_signed_in? |
Safe wrapper for user_signed_in? that returns false when Devise is not installed |