【问题标题】:Why does Auth0 reset Rails session?为什么 Auth0 会重置 Rails 会话?
【发布时间】:2020-03-24 14:28:07
【问题描述】:

我有一个与 Rails 应用程序的简单 auth0 集成,我正在尝试在会话中保留 account_id。我注意到,如果我使用本地登录,它会正确设置会话。如果我使用社交提供者,它将删除会话中的userinfoaccount_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。我直接从示例应用程序中复制了代码,但它不起作用。

【问题讨论】:

    标签: ruby-on-rails oauth auth0


    【解决方案1】:

    我有一个解决方法,就是将该数据存储在加密的 cookie 中。

    cookies.encrypted[:account_id] = result[:model].uuid
    

    我不喜欢它,但它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-20
      • 2011-08-11
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多