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:
Now test signing in by making a request like this:
Three of the headers will be used while testing sign out.
- client
- uid
- access-token
Grab all of these and make a request like this:
Now you know how to use Devise token authentication in Ruby on Rails 6! Happy authenticating!