Table of Contents

Introduction

We all know how API authentication using JWT has become important while building any application. Are you stuck with your JWT authentication? Do you want to learn how to build Rails API authentication using JWT? Then this guide is for you!

Prerequisites to Build Rails API Authentication Using JWT

We will build a simple demo application implementing the Login feature and see how to implement JWT-based authentication in the Rails app. Here’s what you need.

  • Ruby 2.7.0
  • Postman
  • Rails 7.0.3
  • SQLite DB

Create Rails Application

To begin with, let’s create a Rails app by using the below command

Copy Text
$ rails new jwt_demo --api 

Add Gems

Now add gems to your project. Here we will need two gems:

  • JWT
    • Open industry standard (RFC 7519)
    • Used to represent claims securely between two parties
  • bcrypt
    • An algorithm used for password hashing
Copy Text
# Use JWT gem for token-based authentication
gem 'jwt'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

Now, install all dependencies by running bundle install.

Looking for an enthusiastic team of ROR developers to shape the vision of your web project?
Bacancy is a one-stop solution for custom app development. Contact us today and hire Ruby on Rails developer from us for your dream project.

Create User Model

The next step in our tutorial is to create a user model. For that use the below command.

Copy Text
rails g model user name:string user_name:string email:string Password_digest:string

Run migration using rails db:migrate

Add the below code to your User Model.

Create User Model

Create User Controller

Now, moving on to creating a controller with the below command.

Copy Text
rails g controller users

Add CRUD Operations

We will be implementing CRUD operations in our application. So, for that refer to the below code.

Add CRUD Operations

Create JwtToken Concern

Now, in this section, we will implement JWT. As shown below we will use these two functions for creating Rails API Authentication using JWT.

Create JwtToken Concern

Add the authenticate_user method inside your application_controller

application_controller

Creating Authentication Controller

Use the below command to create an authentication controller.

Copy Text
rails g controller authentication

Implementing the Login Function

Now, we will implement the login function in authentication_controller as shown in the below code.

Implementing the Login Function

Update routes.rb

Use the code to update the route.

Update routes.rb

Test the API using Postman

So, we are done with building our demo application. It’s time to test now!

Create Users

Create Users

Login

Login

Conclusion

So, this was a beginner’s tutorial on how to build Rails API authentication using JWT. We hope the step-by-step guideline has helped you achieve the basics of JWT-based authentication. For more such tutorials visit the ROR tutorials page and explore the fundamentals of Ruby on Rails.

Need Help to Build Rails API Authentication using JWT?

Book a Free 30 Min Call to Know How

Build Your Agile Team

Hire Skilled Developer From Us

[email protected]

Your Success Is Guaranteed !

We accelerate the release of digital product and guaranteed their success

We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.

How Can We Help You?