简体   繁体   English

设计+子域 - 重定向用户sign_in

[英]Devise + Subdomain - Redirect user for sign_in

I am using devise gem in a rails application with multiple subdomains. 我在具有多个子域的rails应用程序中使用devise gem。 Each subdomain is handled by respective controller, which look like this: 每个子域由相应的控制器处理,如下所示:

class Subdomain1Controller < ApplicationController
  before_filter :authenticate_user!
  def index
  end
end

With above controller implementation, Devise always keep subdomain while redirecting user to login page. 通过上述控制器实现,Devise始终保持子域,同时将用户重定向到登录页面。 In above case, Devise redirect user to http://subdomain1.acmesite/users/sign_in instead of a common sign_in Url. 在上述情况下,Devise将用户重定向到http://subdomain1.acmesite/users/sign_in而不是常用的sign_in Url。

This leads to having multiple sign_in urls for each sub-domains. 这导致每个子域具有多个sign_in URL。

http://subdomain1.acmesite/users/sign_in
http://subdomain2.acmesite/users/sign_in
http://subdomain3.acmesite/users/sign_in

I am wondering if it's possible to override devise method to exclude subdomain part from the url and yet keeping the previous page url information. 我想知道是否可以覆盖设计方法从网址中排除子域部分,但保留上一页的网址信息。 More preciously, I wants Devise to redirect user to a specific Url (like: http://acmesite/users/sign_in ) irrespective of subdomain and after successful authentication, Devise should return user back to the caller subdomain+page. 更珍贵的是,我希望Devise将用户重定向到特定的Url(例如: http:// acmesite / users / sign_in ),无论子域名如何,并且在成功进行身份验证后,Devise应该将用户返回给调用者子域+页面。

You need to write a custom FailureApp which kicks in when the user is unauthenticated. 您需要编写一个自定义的FailureApp,它在用户未经身份验证时启动。

From How To: Redirect to a specific page when the user can not be authenticated 如何:当用户无法进行身份验证时,重定向到特定页面

class CustomFailure < Devise::FailureApp
  def redirect_url
    #return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scope
     new_user_session_url(:subdomain => 'secure')
  end

  # You need to override respond to eliminate recall
  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

And add the following in config/initializers/devise.rb: 并在config / initializers / devise.rb中添加以下内容:

config.warden do |manager|
  manager.failure_app = CustomFailure
end

If you're getting an uninitialized constant CustomFailure error, and you've put the CustomFailure class under your /lib directory, make sure to autoload your lib files in your application.rb file, like below 如果您收到未初始化的常量CustomFailure错误,并且已将CustomFailure类放在/ lib目录下,请确保在application.rb文件中自动加载您的lib文件,如下所示

config.autoload_paths += %W(#{config.root}/lib)

I droped Devise gem from my project and now using Sorcery instead. 我从我的项目中删除了Devise gem,现在使用了Sorcery。

Sorcery provide me complete control over controller and view, fully comply with my project requirements. 巫术为我提供了对控制器和视图的完全控制,完全符合我的项目要求。 With six months running on production after this transition, I am happy with Sorcery gem. 在这次过渡后,我有六个月的生产时间,我对Sorcery宝石很满意。

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

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