Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将使用Google OAuth2.0登录限制为Ruby上的特定白名单域名_Ruby On Rails_Devise_Oauth 2.0 - Fatal编程技术网

Ruby on rails 将使用Google OAuth2.0登录限制为Ruby上的特定白名单域名

Ruby on rails 将使用Google OAuth2.0登录限制为Ruby上的特定白名单域名,ruby-on-rails,devise,oauth-2.0,Ruby On Rails,Devise,Oauth 2.0,编辑:我改变了我想这样做的方式,我想我会使用一个MySQL表来将使用google的Desive登录白名单。更改后的问题发布在此处: 好的,所以我正在尝试使用Desive和Omni-Auth2以及google为我的ruby on rails网站获得受限身份验证。到目前为止一切正常,但我只希望来自某个域的电子邮件被接受。我愿意做这件事 我做了一些谷歌搜索,但似乎有些PHP用户比我有更多的本地文件,可能是因为在本地使用了GoogleAPI客户端?我不太确定,因为我对一般的编码还是比较陌生的,而且很惊讶

编辑:我改变了我想这样做的方式,我想我会使用一个MySQL表来将使用google的Desive登录白名单。更改后的问题发布在此处:

好的,所以我正在尝试使用Desive和Omni-Auth2以及google为我的ruby on rails网站获得受限身份验证。到目前为止一切正常,但我只希望来自某个域的电子邮件被接受。我愿意做这件事

我做了一些谷歌搜索,但似乎有些PHP用户比我有更多的本地文件,可能是因为在本地使用了GoogleAPI客户端?我不太确定,因为我对一般的编码还是比较陌生的,而且很惊讶我能做到这一点

以下是一个例子:

在这里:

两者似乎都使用“hd:domain”或类似的东西,但这似乎有问题,加上我不确定如何在我的应用程序中实现它

现在想了解更多信息,我只使用gemdesigne和omniauth-google-oauth2(),我觉得有一种方法可以用gem实现,但仍然不能完全确定。任何帮助将不胜感激,如果我可以张贴任何更多的信息让我知道

我的omniauth\u回调\u控制器:

class User::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.google_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end
class User::OmniAuthCallbackController“Google”
登录并重定向@user,:event=>:身份验证
其他的
会话[“designe.google_data”]=request.env[“omniauth.auth”]
将\重定向到新\用户\注册\ url
结束
结束
结束

为什么不在模型中添加验证来限制域

validates :email,
           presence: true,
           uniqueness: true,
           format: {
                   message: 'domain must be example.com',
                   with: /\A[\w+-.]+@example.com\z/i
                   }
其他用户回答如下:


我知道这个问题很老,但回答它只是为了参考。您需要更改config/initializer/omniauth.rb并将“hd”添加到提供程序中

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV["google_client_id"], ENV["google_client_secret"],
           {
               hd: 'domain.com'
           }
end

我也做过同样的事,但对我不起作用。。如果您有其他解决方案,请在这里发布