Devise helps you add user login and registration features to your Rails app quickly and safely.
Devise gem overview in Ruby on Rails
gem 'devise'
bundle install
rails generate devise:install
rails generate devise User
rails db:migrateFirst, add Devise to your Gemfile and install it with bundler.
Then generate Devise configuration and a User model with authentication features.
gem 'devise'rails generate devise:install
rails generate devise User
rails db:migrate
This example shows how to add Devise to a Rails app, create a User model, and require login for all pages. It also shows how to display login/logout links in the layout.
# Gemfile source 'https://rubygems.org' gem 'rails', '~> 7.0' gem 'devise' # After running bundle install # Terminal commands: # rails generate devise:install # rails generate devise User # rails db:migrate # app/controllers/application_controller.rb class ApplicationController < ActionController::Base before_action :authenticate_user! end # app/views/layouts/application.html.erb <% if user_signed_in? %> <p>Welcome, <%= current_user.email %>!</p> <%= link_to 'Logout', destroy_user_session_path, method: :delete %> <% else %> <%= link_to 'Login', new_user_session_path %> | <%= link_to 'Sign up', new_user_registration_path %> <% end %>
Devise handles many security details for you, like password encryption.
You can customize which features (modules) Devise uses, like confirmable or lockable.
Remember to add before_action :authenticate_user! to protect pages.
Devise is a ready-made tool to add user login and registration to Rails apps.
It saves time and improves security by handling common authentication tasks.
Use Devise commands to set up and customize user authentication easily.