编辑:我改变了我想要这样做的方式我想我会使用MySQL表将设计登录列入白名单。已更改的问题发布在此处:Restrict Login with Google OAuth2.0 and Devise to Specific Whitelist Table using Ruby
好吧所以我试图在rails网站上使用Devise和Omni-Auth2来获取受限制的身份验证,只有google。到目前为止一切正常,但我只希望接受来自某个域的电子邮件。无论如何我都愿意这样做。
我做了一些谷歌搜索,但似乎有些PHP用户比我更多的本地文件,可能是因为在本地使用谷歌API客户端?我并不完全确定,因为我对编码一般都很陌生并且很惊讶我做到了这一点。
以下是一个示例:Google Oauth2.0 with Python: How do I limit access to a specific domain?
在这里:Restrict Login Email with Google OAuth2.0 to Specific Domain Name
两者似乎都使用“hd:domain”或类似的东西,但似乎存在问题,而且我不确定如何在我的应用中暗示它。
现在获取更多信息,我只使用gem devise和omniauth-google-oauth2(https://github.com/zquestz/omniauth-google-oauth2)我觉得这是一种用这种宝石做的方法,但仍然不完全确定。如果我可以发布任何更多信息让我知道,任何帮助将不胜感激。
我的omniauth_callbacks_controller:
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
答案 0 :(得分:2)
我知道这个问题很老但回答它只是为了参考。您需要更改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
答案 1 :(得分:1)
为什么不直接在模型中添加验证来限制域?
validates :email,
presence: true,
uniqueness: true,
format: {
message: 'domain must be example.com',
with: /\A[\w+-.]+@example.com\z/i
}
其他用户在这里回答:
Restrict Login with Google OAuth2.0 and Devise to Specific Whitelist Table using Ruby