Installing and Configuring Devise for User Authentication
Table of contents
No headings in the article.
Introduction: Devise is a popular and flexible authentication gem for Ruby on Rails applications. It provides a full suite of authentication features, including user registration, password recovery, and sign-in/sign-out functionality. In this tutorial, we will walk through the process of installing and configuring Devise in a Rails 7 application.
Prerequisites:
Ruby 3.0 or newer
Rails 7.0 or newer
PostgreSQL 14 or newer
Step 1: Create a new Rails application Create a new Rails application by running the following command:
rails new my_app --database=postgresql
Change your working directory to the newly created application:
cd my_app
Step 2: Add Devise to your Gemfile Add the Devise gem to your Gemfile:
gem 'devise'
Then, run the bundle install
command to install the gem:
bundle install
Step 3: Run Devise generator Run the following command to generate the Devise configuration files:
rails generate devise:install
This command will create an initializer file, config/initializers/devise.rb
, and a set of locale files for internationalization support.
Step 4: Configure Devise settings Edit the config/initializers/devise.rb
file and set the config.mailer_sender
to an appropriate email address for your application:
config.mailer_sender = 'noreply@example.com'
In config/environments/development.rb
, add the following line to configure the default URL options for the Devise mailer:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Step 5: Generate a User model with Devise Run the following command to generate a User model with Devise:
rails generate devise User
This will create a User model and migration file, as well as views and controllers for authentication.
Step 6: Run migrations Run the following command to apply the generated migration:
rails db:migrate
Step 7: Add Devise views and routes Run the following command to generate Devise views:
rails generate devise:views
In config/routes.rb
, add the following line to define Devise routes for the User model:
devise_for :users
Step 8: Add authentication to your controllers To require authentication for specific controllers, add the following line at the beginning of the controller:
before_action :authenticate_user!
Step 9: Add navigation links In your application layout (app/views/layouts/application.html.erb
), add links for user authentication:
<% if user_signed_in? %>
<%= link_to 'Edit profile', edit_user_registration_path %> |
<%= link_to 'Logout', destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to 'Sign up', new_user_registration_path %> |
<%= link_to 'Login', new_user_session_path %>
<% end %>
Step 10: Test your application Start your Rails server:
rails server
Visit http://localhost:3000/users/sign_up
in your browser to create a new user and test the authentication functionality.
Conclusion: You have now successfully installed and configured Devise in your Rails 7 application. With the provided code samples and instructions, you can easily integrate user authentication into your app and customize the authentication process to suit your needs.