ruby-on-rails - 为什么 Auth0 会重置 Rails 会话?
问题描述
我有一个与 Rails 应用程序的简单 auth0 集成,我正在尝试将其保留account_id
在会话中。我注意到,如果我使用本地登录,它会正确设置会话。如果我使用社交提供者,它将删除会话中的userinfo
和account_id
。为什么以及如何?
有趣的是,在另一个控制器操作中,如果我设置相同的键,会话将持续存在。
class Auth0Controller < ApplicationController
def callback
session[:userinfo] = request.env['omniauth.auth']
account_attributes = session.dig :userinfo, :extra, :raw_info
result = Accounts::Create.(params: account_attributes)
session[:account_id] = result[:model].uuid
redirect_to root_path
end
def failure
@error_msg = request.params['message']
end
end
应用控制器:
class ApplicationController < ActionController::Base
# protect_from_forgery with: :exception
end
发展.rb
...
if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
我试过了:
- 使用多个浏览器
- 使用不同的 Oauth 提供商(Facebook 和 LinkedIn)
- 不同的会话存储
config.session_store :cookie_store, key: 'my_session'
我现在最好的猜测是,这与 auth0 回调控制器特别有关。我已经设置了应用程序并且那些成功登录的用户,但他们不会userinfo
在会话中持久化。我直接从示例应用程序中复制了代码,但它不起作用。
解决方案
我有一个解决方法,就是将该数据存储在加密的 cookie 中。
cookies.encrypted[:account_id] = result[:model].uuid
我不喜欢它,但它有效。
推荐阅读
- python - 我有在熊猫中找到负面结果的问题
- amazon-dynamodb - get-item 命令的正确语法,适用于 aws 的 dynamodb
- docker - VSCode 无法访问 wsl2 镜像中的 Docker 镜像
- prolog - Prolog:如何根据类别将列表分成两个列表?
- tfs - TFS - 手动将服务器同步到本地
- java - 从 api 获取数据时出现意外的响应代码 403
- c# - 如何在 C# 中调用使用指针作为参数的 DLL 函数 (C++)?
- reactjs - React 应用程序(或任何 SPA)应该如何与 HATEOAS 后端交互?
- java - 为什么 ThreadLocalMap.Entry 扩展 WeakReference?
- swift - 如何在 Swift 的静态函数中调用一次函数?