错误味精:
Error: Request failed with status code 500
Stack trace:
[42]/</t.exports@http://crdwk.herokuapp.com/packs/bundle-ecc8ea14dbe153e50352.js:1:89311
[42]/</t.exports@http://crdwk.herokuapp.com/packs/bundle-ecc8ea14dbe153e50352.js:1:251725
[42]/</t.exports/</d[h]@http://crdwk.herokuapp.com/packs/bundle-ecc8ea14dbe153e50352.js:1:88311
Ruby版本:2.3
Rails版本:5.1
我有一个服务器端渲染,客户端水合React / Rails应用程序(使用gem 'react-rails'
)。
我在gem 'rack-cors'
中添加了application.rb
plus设置,以便我的工作请求(我正在使用axios
)。但是,signing out(DELETE请求)失败并且命中刷新会擦除当前用户。这两个问题都不在本地/正在开发中。
这是应用程序:http://crdwk.herokuapp.com
我看了看我的Heroku日志:
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `reset_token' for nil:NilClass):
app/controllers/application_controller.rb:29:in `sign_out'
app/controllers/api/sessions_controller.rb:3:in `destroy'
鉴于打击刷新,当前用户不会持久,问题是当前用户不知何故未设置。
但是,这不是开发中的问题。那为什么会这样?
通过我的项目,我可以在后端找到的唯一区别与客户端渲染的(我从字面上复制和粘贴此项目的代码)相比,这是application_controller.rb
中的这一行:
skip_before_action :verify_authenticity_token
但是,如果我注释掉这一行,当我尝试注册时,我收到服务器错误
Started POST "/api/users" for 127.0.0.1 at 2018-03-05 12:16:57 -0800
Processing by Api::UsersController#create as JSON
Parameters: {"user"=>{"email"=>"", "password"=>"[FILTERED]"}}
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
这是使用gem 'react-rails'
的结果。 (我的其他客户端渲染项目没有出现此错误。)
没有"authenticity_token"
参数。
我找到了这两个资源:Rails security和Learnetto's how-to。
所以我将第二篇文章中的这两行代码添加到我的qazxsw poi中:
api.js
现在我的网络应用程序使用额外的const csrfToken = document.querySelector("meta[name=csrf-token]").content;
axios.defaults.headers.common["X-CSRF-Token"] = csrfToken;
线注释掉了。除了,我不能使用DOM来获取我的React Native版本的csrf令牌,所以我现在对移动设备有同样的问题...