Rails 5使用Devise和acts_as_tenant

3

我有一个应用程序,正在使用Devise来处理登录/注册。我还在使用acts_as_tenant。我需要确保每次有人注册/登录时都设置了租户。为了让acts as tenant工作,必须在认证之前设置租户。现在我在我的ApplicationController上使用了一个before_action,但问题是,即使有人有无效的凭证等,该方法也会被调用,我试图避免在该方法中编写if语句来确定是否有有效的用户。

如何最好地实现这一点?有类似情况的人吗?

1个回答

0
如果您需要在使用 Devise 进行身份验证之前设置租户,您需要使用 acts_as_tenant 的手动租户设置方法,以便您可以控制时机。
# application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  # tell acts_as_tenant you'll manually set the tenant 
  set_current_tenant_through_filter 

  # manually set the tenant before Devise auth
  prepend_before_action :set_current_tenant_by_subdomain

  # Devise auth as usual but after tenant is set
  before_action :authenticate_user!


  private

  def set_current_tenant_by_subdomain
    current_organization = Organization.find_by_subdomain(request.subdomain)
    set_current_tenant(current_organization)
  end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接