LoginSignup
5
1

More than 3 years have passed since last update.

Rails-APIでsorceryを使ったらundefined local variable or method `form_authenticity_token'と怒られた

Last updated at Posted at 2020-04-03

概要

Rails-APIにて認証用のgem sorceryを使ってloginメソッドを叩いた時に

undefined local variable or method `form_authenticity_token'

と怒られたので解決策を書きます。

環境

ruby 2.6.5
Rails 6.0.2.1
sorcery (0.14.0)

結論

ログイン処理を行うコントローラーに下記を追加すればOK

sessions_controller.rb
private

def form_authenticity_token; end

原因

こちらの記事によると、ユーザーがログインする際にCSRFトークンがリセットされるのですが、それを行うform_authenticity_tokenメソッドがRails-APIに存在しないかららしいです。

このメソッドがないと動作が保証されないので、とりあえず自前で空メソッドを用意してくれ、とのことでした。

見た目はブサイクになりますが仕方なくこれで対応して無事に動きました。

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