Ruby on rails 通过Rails中的设计重定向后如何登录用户?
我想先重定向一个用户,然后以编程方式登录该用户 我有一个multi-tanancy应用程序,当用户尝试在根域登录时,我的应用程序不仅应该将用户重定向到他的子域,而且应该让用户登录 到目前为止,我正试图通过以下代码实现这一点:Ruby on rails 通过Rails中的设计重定向后如何登录用户?,ruby-on-rails,authentication,devise,multi-tenant,apartment-gem,Ruby On Rails,Authentication,Devise,Multi Tenant,Apartment Gem,我想先重定向一个用户,然后以编程方式登录该用户 我有一个multi-tanancy应用程序,当用户尝试在根域登录时,我的应用程序不仅应该将用户重定向到他的子域,而且应该让用户登录 到目前为止,我正试图通过以下代码实现这一点: # In sessions#create ( Root ) redirect_to "http://#{account.subdomain}.localhost.com:3000/users/sign_in" Apartment::Tenant.switch(account
# In sessions#create ( Root )
redirect_to "http://#{account.subdomain}.localhost.com:3000/users/sign_in"
Apartment::Tenant.switch(account.subdomain)
sign_in(:user, account.owner) # Basically, owner is the user here.
当我运行代码时,它成功地重定向到子域,但没有让用户登录
我得到以下错误:
Completed 302 Found in 93ms (ActiveRecord: 39.7ms)
另外,当我运行公寓::租户.switch(account.subdomain)
时,它不会切换模式
,因为在运行公寓::租户.switch(account.subdomain)
后,当我运行user=user时,可以在日志中看到的查询是:
User Load (0.9ms) SELECT "public"."users".* FROM "public"."users" ORDER BY "public"."users"."id" ASC LIMIT 1
这里,public
对应于根域的schema
,而不是子域的schema
经过大量搜索后(唯一)的解决方案是修改config/initializers/session_store.rb中的以下行:
Rails.application.config.session_store :cookie_store, key: '_examples_session', :domain => :all
无论如何,这都不是一个完美的解决方案,它会起作用的。它实际上是通过所有子域维护一个会话
例如,如果我登录到bugs.example.com
,我的会话也会在attention.example.com
和类似的子域中工作
我还写了一个,检查用户试图登录的子域是否属于该用户,如果是,我让用户登录,如果不是,我将用户重定向到他所属的子域,如果用户不属于任何子域,我将他重定向到根url。同样,通过一次会话
,用户将能够登录到每个子域,因此这种检查非常重要