2

我正在使用Apartment and Devise gem 进行多租户和身份验证。

sign_up在根域 URL( example.com) 中有一个页面,可以从用户那里获取子域详细信息。

sign_in成功保存记录并重定向到新的子域(sub.example.com)后,我需要用户。

公寓模式:

帐户 => 所有模式的通用(公共)

用户 => 为每个模式单独创建(私有)

注册控制器

class Users::RegistrationsController < Devise::RegistrationsController
  
  ...
  ...

  def create
    ActiveRecord::Base.transaction do
      @account = Account.new(account_params)
      if @account.valid?
        # Create & switch to the new tenant
        Apartment::Tenant.create(@account.subdomain)
        Apartment::Tenant.switch!(@account.subdomain)
        @account.save

        sign_in(:user, @account.user)
        redirect_to root_url(subdomain: "#{Apartment::Tenant.current}")
      else
        render :action => 'new'
      end
    end
  end

  ...
  ...

  protected
  def account_params
    params.require(:account).permit(:name, :subdomain, user_attributes: [:first_name, :last_name, :email, :password, :password_confirmation])
  end
end

上面的代码成功地重定向到了新的子域,但是,虽然我在重定向之前正在登录用户,但它不是用户登录。

请任何人帮助我将用户重定向signed_in到新的子域。

谢谢..

4

1 回答 1

0

最后,通过添加或我在这个答案中找到:domain => :all的 rootdomain:domain => 'example.com'选项解决了这个问题。session_store

配置/初始化程序/session_store.rb

config.session_store :cookie_store, :key => '_domain_session', :domain => :all

(或者)

config.session_store :cookie_store, :key => '_domain_session', :domain => 'example.com'
于 2017-01-04T14:08:25.240 回答