1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

devise とdevise token auth を共存させるポイント

Posted at
routes.rb
Rails.application.routes.draw do
  # お客様
  devise_for :customers, controllers: {
    sessions: 'customers/sessions',
    passwords: 'customers/passwords'
  }
  resources :companies, only: [:show, :new, :create, :index] do
    # お客様登録
    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'
          # ログイン /api/v1/customer_auth/sign_in
          # パスワード変更 /api/v1/customer_auth/password
          # 認証メール再送信  /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   

deviseを使ってその後に、devise token auth を導入したらActionController::Baseからの引用にしなければ行けなかった。
また、include DeviseTokenAuth::Concerns::SetUserByTokenがないと、before_action :authenticate_customer!に対して、ヘッダーにclientとaccece-tokenを入れても、認証エラーになってしまいます。

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 
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?