• Build an easy to use full-featured option to handle User Auth with a Rails API

$ rails new devise_auth_app --api

Once everything is installed move into the rails projectcd devise_auth_appin your ./Gemfilelet’s install the required gems

gem 'devise'
gem 'jwt'

# we also need to uncomment
gem 'rack-cors'

and now run$ bundle install

Now that we have the required gems in our app let’s run the devise installer.

$ rails g devise:install

When running this, you will get this output in the console, don’t worry about any of this now.


Some setup you must do manually if you haven't yet:
1\. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
In production, :host should be set to the actual host of your application.
2\. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:
root to: "home#index"
3\. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:
<p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>
4\. You can copy Devise views (for customization) to your app by running:
rails g devise:views

Build out the user model

One excellent feature of Devise is that it builds a full User model for you out of the box and can also be edited if needed. In only one line!

$ rails g devise User

Open your db/migrations/ folder, and you will see the user migration that was generated by Devise.

# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at
## Rememberable
      t.datetime :remember_created_at
## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.string   :current_sign_in_ip
      # t.string   :last_sign_in_ip
## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable
## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at
t.timestamps null: false
add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true

For now, let’s leave this alone; if you want to add more values, check out the Devise documentation.

#authentication #devise #ruby-on-rails #jwt-auth #api

A step by step guide to setting up user authentication in Ruby on Rails with JWT...
1.55 GEEK