Authentication using Devise in Rails 7
Tags: ruby, rails, webdev, beginners
Published: 2023-06-06 03:58:32

## Introduction

In this article, we will explore how to implement authentication in a Rails 7 application using the popular [devise gem](https://github.com/heartcombo/devise). Authentication is a crucial aspect of web development, allowing users to securely access and interact with your application. By following this step-by-step guide, you will learn how to set up devise, configure authentication routes, create user models, and enhance your application with authentication features.

---

## Prerequisites

Before we dive into the implementation, make sure you have the following set up on your development environment:

- Ruby  _(version >= 2.7.0)_
- Rails _(version >= 7.0.0)_
- Bundler gem

Let's get started!

---

## Step 1: Creating a New Rails Application

First, let's create a new Rails application. Open your terminal and execute the following command:

```
rails new authentication-app

# If you want to use a CSS Framework
# rails new authentication-app -c [tailwind|bootstrap|bulma|postcss|sass]

cd authentication-app
```

---

## Step 2: Adding Devise to the Gemfile

Next, let's add the Devise gem to our application's Gemfile. Open the `Gemfile` using a text editor and add the following line:

```
gem 'devise'
```

Save the file and run the following command in your terminal to install the gem:

```
bundle install
```

---

## Step 3: Setting up Devise

To set up Devise in our application, we need to run a few generator commands. Execute the following command in your terminal:

```
rails generate devise:install
```

Upon running this command, you will see some instructions in your terminal. I have listed them here, so that you don't have to worry about them 🙂 Just follow the below steps:

**1:** Add the below code in `config/environments/development.rb`:

```
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
```

**2:** Devise recommends to have a root url in the application. To do this, we need to generate a controller and a view:

```
rails g controller home index
```

This will generate controller with the name `home` and a view `index.html.erb`. 

Now, add the below route in your `config/routes` file:

```
root 'home#index'
```

**3:** Add the below code above `<%= yield %>` tag in your `app/views/layouts/application.html.erb`:

```
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
```

This will display the flash notifications.

**4:** To generate views for authentication(login, signup etc.) pages, run the following command:

```
rails g devise:views
```

---

## Step 4: Creating a User Model

Let's create a User model using Devise. Run the following command:

```
rails generate devise User
```

This will generate a User model with Devise's authentication features.

After generating the model, run migrations:

```
rails db:migrate
```

---

## Step 5: Adding Authentication Links to Views

Now, let's add authentication links to our views. Open your application's layout file (`app/views/layouts/application.html.erb`) and paste the following code:

```
<% if user_signed_in? %>
  <%= button_to "Sign Out", destroy_user_session_path, method: :destroy %>
<% else %>
  <%= link_to 'Sign In', new_user_session_path %>
  <%= link_to 'Sign Up', new_user_registration_path %>
<% end %>
```

This code will display **"Sign Out"** if the user is signed in, and **"Sign In"** and **"Sign Up"** if the user is not signed in.

---

## Last Step: Testing Authentication

At this point, we have set up authentication in our Rails application using Devise. You can now start your Rails server (rails server) and navigate to the appropriate routes to test the authentication functionality.

---

## Conclusion

Congratulations! You have successfully implemented authentication in your Rails 7 application using the Devise gem. With Devise, you can easily handle user registration, sign-in, and sign-out processes. This step-by-step guide should have provided you with a solid foundation to build upon and customize authentication features according to your application's needs.

Remember to explore [Devise's documentation](https://github.com/heartcombo/devise) for further customisation options and advanced features. 

Happy coding! ✨