[RAILS] Points for coexistence of devise and devise token auth

routes.rb


Rails.application.routes.draw do
  #Customer
  devise_for :customers, controllers: {
    sessions: 'customers/sessions',
    passwords: 'customers/passwords'
  }
  resources :companies, only: [:show, :new, :create, :index] do
    #Customer registration
    devise_for :customers, controllers: {
      registrations: 'customers/registrations'
    }
  end

  namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'Customer', at: 'customer_auth', controllers: {
          registrations: 'api/v1/customers/registrations',
          passwords: 'api/v1/customers/passwords',
          sessions: 'api/v1/customers/sessions',
          confirmations: 'api/v1/customers/confirmations'
          #Login/api/v1/customer_auth/sign_in
          #Change Password/api/v1/customer_auth/password
          #Resend verification email/api/v1/customer_auth/confirmation
      }
    end
  end
...

application_controller.rb


class ApplicationController < ActionController::Base
    protect_from_forgery with: :null_session, if: -> {request.format.json?}
end   

After using devise and then introducing devise token auth, I had to quote from ActionController :: Base. Also, without include DeviseTokenAuth :: Concerns :: SetUserByToken, even if you put client and accece-token in the header forbefore_action: authenticate_customer!, An authentication error will occur.

api/vi/application_controller.rb


module Api
    module V1
      # class ApplicationController < ActionController::API # Note: here is not ::BASE
      class ApplicationController < ActionController::Base
        include DeviseTokenAuth::Concerns::SetUserByToken
        protect_from_forgery with: :null_session
        respond_to :json
        end
      end
    end
end 

Recommended Posts

Points for coexistence of devise and devise token auth
Compare the speed of the for statement and the extended for statement.
Criteria for proper use of render and redirect_to
[For beginners] DI ~ The basics of DI and DI in Spring ~
List of frequently used Java instructions (for beginners and beginners)