[英]Devise: Redirect authenticated users to a single subdomain in Rails 5
我有一个使用Devise进行用户身份验证的Rails 5应用程序,当前在主域和子域之间共享用户会话。
我想更改此行为,以便将经过身份验证的用户路由到子域sub.example.com
而将未经身份验证的用户保留在主域example.com
。
我发现的解决方案着重于多租户实现,但是这不是多租户应用,因此我现在只需要将用户重定向到单个子域。
我目前正在使用自定义Devise方法将经过身份验证的用户重定向到所请求的资源,具体取决于其角色:
def after_sign_in_path_for(resource)
if current_user.try(:guest?)
documents_path
else
stored_location_for(resource) || dashboard_path
end
end
这是可以通过其他应用程序控制器过滤器处理的事情,还是可能以其他方式处理?
可以覆盖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
然后您将在config/initializers/session_store.rb
需要类似的东西
MyApp::Application.config.session_store :cookie_store, domain: '.maindomain.com'
或者,如果您只想要一个特定子域的会话:
MyApp::Application.config.session_store :cookie_store, domain: 'something.rootdomain.com'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.