Ruby on Rails web application two-factor authentication Devise security user accounts password login additional information phishing protection OTP encryption key

How to Implement Two-Factor Authentication in Your Ruby on Rails Web Application

2023-05-01 11:16:20

//

5 min read

Blog article placeholder

How to Implement Two-Factor Authentication in Your Ruby on Rails Web Application

Two-factor authentication (2FA) is now becoming a standard security measure for web applications. It adds an extra layer of security to user accounts by requiring users to provide an additional piece of information besides their password during login.

In this article, we’ll walk you through how to implement two-factor authentication in your Ruby on Rails web application.

Step 1: Install Two-Factor Authentication Gem

The first step is to install the two-factor authentication gem named "Devise". Devise is a flexible authentication solution for Rails based on Warden. Devise provides secure password storage, phishing protection, and more.

You can add Devise to your Gemfile by:

``` gem ‘devise’ ```

After adding the above line, run the command:

``` bundle install ```

Step 2: Generate Devise Install

Now that Devise is installed, you need to generate the Devise configuration files by running:

``` rails generate devise:install ```

After running this, Devise will create the necessary files.

Step 3: Create Users and Add Devise to Users

In this step, we’ll create the user model and add Devise to it by running:

``` rails generate devise User ```

This will create the model "user.rb" and some "Devise" related configuration in it.

Run the following command to migrate the newly created database:

``` rake db:migrate ```

Step 4: Add Two-Factor Authentication

The next step is to add two-factor authentication to your Rails application.

Run the following command to install the Devise two-factor authentication gem:

``` rails generate devise_two_factor:install User 2fa ```

This will generate two-factor authentication files in your application, including:

  • A migration to add the two-factor authentication columns to the users table
  • A model for two-factor authentication
  • An initializer for configuration
  • View templates for OTP input and QR code generation

Step 5: Implement Two-Factor Authentication

The final step is to implement two-factor authentication in your Rails application.

In your "user" model, add the following line:

``` devise :two_factor_authenticatable, :otp_secret_encryption_key => ENV['TWO_FACTOR_ENCRYPTION_KEY'] ```

In the controller, you need to enable two-factor authentication with:

``` before_action :authenticate_user!, :authenticate_user_with_two_factor, :unless => :devise_controller? ```

The last step is to add a link to let users enable two-factor authentication. You can add it to your preference page or create a new one.

Conclusion

In this article, we’ve shown you how to implement two-factor authentication in your Ruby on Rails web application using Devise. By following these steps, you can add an extra layer of security to your user’s accounts and protect them against potential security breaches.