{"id":30761,"date":"2022-09-23T10:33:36","date_gmt":"2022-09-23T10:33:36","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=30761"},"modified":"2025-11-26T10:51:48","modified_gmt":"2025-11-26T10:51:48","slug":"build-rails-api-authentication-using-jwt","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/build-rails-api-authentication-using-jwt","title":{"rendered":"How to Build Rails API Authentication using JWT?"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>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 <b>Rails API authentication using JWT<\/b>? Then this guide is for you!<\/p>\n<h2>Prerequisites to Build Rails API Authentication Using JWT<\/h2>\n<p>We will build a simple demo application implementing the Login feature and see how to implement JWT-based authentication in the Rails app. Here\u2019s what you need.<\/p>\n<ul class=\"bullets text-left\">\n<li>Ruby 2.7.0<\/li>\n<li>Postman<\/li>\n<li>Rails 7.0.3<\/li>\n<li>SQLite DB<\/li>\n<\/ul>\n<h2>Create Rails Application<\/h2>\n<p>To begin with, let\u2019s create a Rails app by using the below command<\/p>\n<pre>$ rails new jwt_demo --api<\/pre>\n<h2>Add Gems<\/h2>\n<p>Now add gems to your project. Here we will need two gems:<\/p>\n<ul class=\"bullets text-left\">\n<li>JWT<br \/>\n  \u2022 Open industry standard (RFC 7519)<br \/>\n  \u2022 Used to represent claims securely between two parties<\/li>\n<li>bcrypt<br \/>\n  \u2022 An algorithm used for password hashing<\/li>\n<\/ul>\n<pre># Use JWT gem for token-based authentication\r\ngem 'jwt'\r\n# Use ActiveModel has_secure_password\r\ngem 'bcrypt', '~> 3.1.7'<\/pre>\n<p>Now, install all dependencies by running bundle install.<\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Looking for an enthusiastic team of ROR developers to shape the vision of your web project?<\/span><br \/>\nBacancy is a one-stop solution for custom app development. Contact us today and <a href=\"https:\/\/www.bacancytechnology.com\/ruby-on-rails-development\" target=\"_blank\" rel=\"noopener\">hire Ruby on Rails developer<\/a> from us for your dream project.<\/i><\/strong><\/p>\n<h2>Create User Model<\/h2>\n<p>The next step in our tutorial is to create a user model. For that use the below command.<\/p>\n<pre>rails g model user name:string user_name:string email:string Password_digest:string<\/pre>\n<p>Run migration using rails <strong>db:migrate<\/strong><\/p>\n<p>Add the below code to your User Model.<\/p>\n<div id=\"attachment_30780\" style=\"width: 1110px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-30780\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-User-Model-min.png\" alt=\"Create User Model\" width=\"1100\" height=\"600\" class=\"size-full wp-image-30780\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-User-Model-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-User-Model-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-User-Model-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-User-Model-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><p id=\"caption-attachment-30780\" class=\"wp-caption-text\">Create User Model<\/p><\/div>\n<h2>Create User Controller<\/h2>\n<p>Now, moving on to creating a controller with the below command.<\/p>\n<pre>rails g controller users<\/pre>\n<h2>Add CRUD Operations<\/h2>\n<p>We will be implementing CRUD operations in our application. So, for that refer to the below code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Add-CRUD-Operations-min.png\" alt=\"Add CRUD Operations\" width=\"1100\" height=\"1198\" class=\"alignnone size-full wp-image-30778\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Add-CRUD-Operations-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Add-CRUD-Operations-min-275x300.png 275w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Add-CRUD-Operations-min-940x1024.png 940w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Add-CRUD-Operations-min-768x836.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h2>Create JwtToken Concern<\/h2>\n<p>Now, in this section, we will implement JWT. As shown below we will use these two functions for creating <i>Rails API Authentication using JWT<\/i>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-JwtToken-Concern-min.png\" alt=\"Create JwtToken Concern\" width=\"1100\" height=\"600\" class=\"alignnone size-full wp-image-30779\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-JwtToken-Concern-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-JwtToken-Concern-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-JwtToken-Concern-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-JwtToken-Concern-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>Add the <mark>authenticate_user<\/mark> method inside your <mark>application_controller<\/mark><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/application_controller-min.png\" alt=\"application_controller\" width=\"1100\" height=\"600\" class=\"alignnone size-full wp-image-30776\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/application_controller-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/application_controller-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/application_controller-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/application_controller-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h2>Creating Authentication Controller<\/h2>\n<p>Use the below command to create an authentication controller.<\/p>\n<pre>rails g controller authentication<\/pre>\n<h2>Implementing the Login Function<\/h2>\n<p>Now, we will implement the login function in authentication_controller as shown in the below code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Implementing-the-Login-Function-min.png\" alt=\"Implementing the Login Function\" width=\"1100\" height=\"600\" class=\"alignnone size-full wp-image-30775\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Implementing-the-Login-Function-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Implementing-the-Login-Function-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Implementing-the-Login-Function-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Implementing-the-Login-Function-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h2>Update routes.rb<\/h2>\n<p>Use the code to update the route.<\/p>\n<div id=\"attachment_30774\" style=\"width: 1110px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-30774\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Update-routes.rb-min.png\" alt=\"Update routes.rb\" width=\"1100\" height=\"600\" class=\"size-full wp-image-30774\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Update-routes.rb-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Update-routes.rb-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Update-routes.rb-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Update-routes.rb-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><p id=\"caption-attachment-30774\" class=\"wp-caption-text\">Update routes.rb<\/p><\/div>\n<h2>Test the API using Postman<\/h2>\n<p>So, we are done with building our demo application. It\u2019s time to test now!<\/p>\n<h3>Create Users<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-Users-min.png\" alt=\"Create Users\" width=\"1100\" height=\"600\" class=\"alignnone size-full wp-image-30773\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-Users-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-Users-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-Users-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Create-Users-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h3>Login<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Login-min.png\" alt=\"Login\" width=\"1100\" height=\"600\" class=\"alignnone size-full wp-image-30772\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Login-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Login-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Login-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/09\/Login-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>So, this was a beginner\u2019s 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 <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/ruby-on-rails\" target=\"_blank\" rel=\"noopener\">ROR tutorials page<\/a> and explore the fundamentals of Ruby on Rails.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":169,"featured_media":30783,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[35],"tags":[],"coauthors":[2426],"class_list":["post-30761","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"modified_by":"Nisarg Bhavsar","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/30761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/169"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=30761"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/30761\/revisions"}],"predecessor-version":[{"id":56744,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/30761\/revisions\/56744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/30783"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=30761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=30761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=30761"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=30761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}