Devise Token Auth in Ruby on Rails 6

This article will discuss how to implement token-based authentication using a gem called devise_token_auth. Here’s how to get started:

1
2
rails new devise_token_auth --api -T
rails db:create

Now add the gem in your Gemfile.

1
gem 'devise_token_auth'

Run bundle install, then run the following command:

1
rails g devise_token_auth:install User auth

Add this line to app/model/user.rb:

1
2
3
# app/model/user.rb

extend Devise::Models

Run rails db:migrate, then test your implementation through Postman. In your terminal, run rails server and open Postman, then make a request like this:

Screen Shot 2020-06-12 at 1.50.01 AM.png

Now test signing in by making a request like this:

Screen Shot 2020-06-12 at 1.54.23 AM.png

Three of the headers will be used while testing sign out.

  1. client
  2. uid
  3. access-token

Grab all of these and make a request like this:

Screen Shot 2020-06-12 at 1.56.22 AM.png

Now you know how to use Devise token authentication in Ruby on Rails 6! Happy authenticating!