LoginSignup
16
20

More than 5 years have passed since last update.

rails5,devise_token_authでのエラー対応もろもろ

Last updated at Posted at 2018-01-24

エラー「Filter chain halted as :authenticate_user! rendered or redirected」

以下ファイルで、 exceptを追記しました。配列内はメソッド名です。

app/controllers/application_controller.rb
# before_filter :authenticate_user!
before_action :authenticate_user!, except: [:new, :create]

Postmanから /auth/sign_in にPOSTして、401が帰ってくることがなくなりました。

続いて出たエラー「Can't verify CSRF token authenticity」

同じく、app/controllers/application_controller.rb での記述を変更したところ、200が帰ってくるようになりました。

app/controllers/application_controller.rb
# protect_from_forgery with: :exception
protect_from_forgery with: :null_session

Can't verify CSRF token authenticity.

いろいろ調べると、

# protect_from_forgery with: :exception
protect_from_forgery with: :null_session

をすると解決すると書いてあるのですが、エラーは消えませんでした。
devise_token_authのcorsに書いてある記述を追記する必要がありました。

config/application.rb
module YourApp
  class Application < Rails::Application
    config.middleware.use Rack::Cors do
      allow do
        origins '*'
        resource '*',
          :headers => :any,
          :expose  => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
          :methods => [:get, :post, :options, :delete, :put]
      end
    end
  end
end

参考

https://github.com/lynndylanhurley/devise_token_auth/issues/603
devise token auth を使って簡単に早くAPIを作る 1

16
20
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
16
20