首页 > 解决方案 > 为什么 Auth0 会重置 Rails 会话?

问题描述

我有一个与 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

我试过了:

我现在最好的猜测是,这与 auth0 回调控制器特别有关。我已经设置了应用程序并且那些成功登录的用户,但他们不会userinfo在会话中持久化。我直接从示例应用程序中复制了代码,但它不起作用。

标签: ruby-on-railsoauthauth0

解决方案


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

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

我不喜欢它,但它有效。


推荐阅读