A Devise-JWT Tutorial For Authenticating Users in Ruby on Rails
So you can auth users from React, Angular or Vue frontend apps
Recently, I worked on a Rails app which authenticated users from a separate ReactJS frontend via JWT tokens.
This is the tutorial I wrote to solidify my understanding on how that process works.
Today we’ll walk through building a Rails backend using Devise-JWT which allows users to log in, access endpoints requiring authentication, and log out.
We’ll only cover hitting endpoints via curl, but the exact same logic applies if making requests from a frontend JS app.
I pieced together about 5 broken tutorials to find a pattern that worked, so I hope you find this useful.
Setup
Create a rails app from the command line.
$ rails new devise-jwt-app -T -d postgresql --api
$ cd devise-jwt-app
Add devise
, devise-jwt
and rack-cors
to the Gemfile
.
gem 'devise'
gem 'devise-jwt'
gem 'rack-cors'
Run bundle to install them.
$ bundle install
config/initializers/cors.rb
is generated by default when using the--api
to create an app. Otherwise you…