RailsをAPIとして使っています。devise token authでトークン認証を導入し、ログイン認証は問題なくできています。しかし、リソースにアクセスしようとすると、ApplicationControllerに存在するメソッドにサブコントローラーからアクセスできず、NoMethodErrorとなってしまいます。
skip_before_action :verify_authenticity_tokenをコメントアウトするしないで、ログインができるできない、リソースにアクセスできるできないが変わってくるようなのですが、どう対処したら良いのでしょうか。
ruby 2.6.6
rails 6.0.3.4
devise 4.7.3
devise_token_auth 1.1.3
Ruby
1エラーメッセージ 2NoMethodError in SubController#index 3undefined method `methodA' for #<SubController:0x0000559c2a82e8c8> 4 5Extracted source (around line #428): 6 7 lambda do |target, value, &block| 8 target, block, method, *arguments = expand(target, value, block) 9 target.send(method, *arguments, &block) 10 end 11end 12
Ruby
1class ApplicationController < ActionController::API 2 include DeviseTokenAuth::Concerns::SetUserByToken 3 skip_before_action :verify_authenticity_token #コメントアウトすると、リソースにアクセスできるが、ログインできない 4 before_action :authenticate_api_user! 5 6 protected 7 def methodA 8 # 略 9 end 10end
Ruby
1class SubController < ApplicationController 2 before_action :methodA, only: [:index] 3 4 def index 5 # 略 6 end 7 8end
Ruby
1DeviseTokenAuth.setup do |config| 2 # By default the authorization headers will change after each request. The 3 # client is responsible for keeping track of the changing tokens. Change 4 # this to false to prevent the Authorization header from changing after 5 # each request. 6 config.change_headers_on_each_request = false 7 8 # By default, users will need to re-authenticate after 2 weeks. This setting 9 # determines how long tokens will remain valid after they are issued. 10 config.token_lifespan = 2.weeks 11 12 # Limiting the token_cost to just 4 in testing will increase the performance of 13 # your test suite dramatically. The possible cost value is within range from 4 14 # to 31. It is recommended to not use a value more than 10 in other environments. 15 config.token_cost = Rails.env.test? ? 4 : 10 16 17 # Sets the max number of concurrent devices per user, which is 10 by default. 18 # After this limit is reached, the oldest tokens will be removed. 19 # config.max_number_of_devices = 10 20 21 # Sometimes it's necessary to make several requests to the API at the same 22 # time. In this case, each request in the batch will need to share the same 23 # auth token. This setting determines how far apart the requests can be while 24 # still using the same auth token. 25 # config.batch_request_buffer_throttle = 5.seconds 26 27 # This route will be the prefix for all oauth2 redirect callbacks. For 28 # example, using the default '/omniauth', the github oauth2 provider will 29 # redirect successful authentications to '/omniauth/github/callback' 30 # config.omniauth_prefix = "/omniauth" 31 32 # By default sending current password is not needed for the password update. 33 # Uncomment to enforce current_password param to be checked before all 34 # attribute updates. Set it to :password if you want it to be checked only if 35 # password is updated. 36 # config.check_current_password_before_update = :attributes 37 38 # By default we will use callbacks for single omniauth. 39 # It depends on fields like email, provider and uid. 40 # config.default_callbacks = true 41 42 # Makes it possible to change the headers names 43 config.headers_names = {:'access-token' => 'access-token', 44 :'client' => 'client', 45 :'expiry' => 'expiry', 46 :'uid' => 'uid', 47 :'token-type' => 'token-type' } 48 49 # By default, only Bearer Token authentication is implemented out of the box. 50 # If, however, you wish to integrate with legacy Devise authentication, you can 51 # do so by enabling this flag. NOTE: This feature is highly experimental! 52 # config.enable_standard_devise_support = false 53end 54
回答1件
あなたの回答
tips
プレビュー