简体   繁体   English

设计:将经过身份验证的用户重定向到Rails 5中的单个子域

[英]Devise: Redirect authenticated users to a single subdomain in Rails 5

I have a Rails 5 app that uses Devise for user authentication and currently shares the user session across the primary and sub domain. 我有一个使用Devise进行用户身份验证的Rails 5应用程序,当前在主域和子域之间共享用户会话。

I would like to change this behavior so that authenticated users are routed to the subdomain sub.example.com while non-authenticated users are kept on the primary domain example.com . 我想更改此行为,以便将经过身份验证的用户路由到子域sub.example.com而将未经身份验证的用户保留在主域example.com

The solutions I've found are focused around multi-tenant implementations, however this is not a multi-tenant app and I only need to redirect users to a single subdomain at this point. 我发现的解决方案着重于多租户实现,但是这不是多租户应用,因此我现在只需要将用户重定向到单个子域。

I am currently using a custom Devise method to redirect authenticated users to the requested resource as determined by his or her role: 我目前正在使用自定义Devise方法将经过身份验证的用户重定向到所请求的资源,具体取决于其角色:

def after_sign_in_path_for(resource)
  if current_user.try(:guest?)
    documents_path
  else
    stored_location_for(resource) || dashboard_path
  end
end

Is this something that would be handled with an additional application controller filter or perhaps another way? 这是可以通过其他应用程序控制器过滤器处理的事情,还是可能以其他方式处理?

It's ok to override the devise method, however you probably wanna move it to your application_controller where you will have access to Rails path helpers 可以覆盖devise方法,但是您可能希望将其移至application_controller,您将在其中访问Rails路径帮助器

class ApplicationController < ActionController::Base

  def after_sign_in_path_for(resource)
    if current_user.try(:guest?)
      documents_path
    else
      stored_location_for(resource) || dashboard_path
    end
  end

  def after_sign_out_path_for(resource)
    #send them somehwere
  end

end

Then you will need something like this in config/initializers/session_store.rb 然后您将在config/initializers/session_store.rb需要类似的东西

MyApp::Application.config.session_store :cookie_store, domain: '.maindomain.com'

Or if you only want sessions for one specific subdomain: 或者,如果您只想要一个特定子域的会话:

MyApp::Application.config.session_store :cookie_store, domain: 'something.rootdomain.com'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM