Google omniauth + 设计 + @domain 访问 + rails
Google omniauth + devise + @domain access + rails
我正在使用 omniauth and devise 和 google 让用户登录网站。我只需要允许用户在拥有特定公司电子邮件的情况下登录。例如,他们单击使用 google 登录,然后除非他们有“@somecompany.com”电子邮件地址,否则他们可以成功登录。否则他们无法使用普通的“@gmail.com”电子邮件登录。我似乎无法在文档中找到执行此操作的位置。
用户模型
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first_or_initialize
user.given_name = data['first_name']
user.family_name = data['last_name']
user.password = SecureRandom.uuid
user.save!
user
end
omniauth 控制器
def google_oauth2
@user = User.from_omniauth(request.env["omniauth.auth"])
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
路线
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
将模型中的方法更新为
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first_or_initialize
user.given_name = data['first_name']
user.family_name = data['last_name']
user.password = SecureRandom.uuid
user.save! unless data['email'].split("@").include?('gmail.com')
user
end
更新google_oauth2
方法以及已经注册的用户
if @user.persisted? && !@user.email.split("@").include?('gmail.com')
你可以试试:
providers:
- { name: 'google_oauth2', app_id: 'APP-ID',
app_secret: 'APP-SECRET',
args: { access_type: 'offline', approval_prompt: 'auto', hd: 'example.com' } }
其中 example.com 已更改为您的公司域。
否则您可以在 Whosebug 上尝试这些答案:
- In Rails, is it possible to limit who can log in with google using the api?
- Restrict Login Email with Google OAuth2.0 to Specific Domain Name
我正在使用 omniauth and devise 和 google 让用户登录网站。我只需要允许用户在拥有特定公司电子邮件的情况下登录。例如,他们单击使用 google 登录,然后除非他们有“@somecompany.com”电子邮件地址,否则他们可以成功登录。否则他们无法使用普通的“@gmail.com”电子邮件登录。我似乎无法在文档中找到执行此操作的位置。
用户模型
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first_or_initialize
user.given_name = data['first_name']
user.family_name = data['last_name']
user.password = SecureRandom.uuid
user.save!
user
end
omniauth 控制器
def google_oauth2
@user = User.from_omniauth(request.env["omniauth.auth"])
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
路线
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
将模型中的方法更新为
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first_or_initialize
user.given_name = data['first_name']
user.family_name = data['last_name']
user.password = SecureRandom.uuid
user.save! unless data['email'].split("@").include?('gmail.com')
user
end
更新google_oauth2
方法以及已经注册的用户
if @user.persisted? && !@user.email.split("@").include?('gmail.com')
你可以试试:
providers:
- { name: 'google_oauth2', app_id: 'APP-ID',
app_secret: 'APP-SECRET',
args: { access_type: 'offline', approval_prompt: 'auto', hd: 'example.com' } }
其中 example.com 已更改为您的公司域。
否则您可以在 Whosebug 上尝试这些答案:
- In Rails, is it possible to limit who can log in with google using the api?
- Restrict Login Email with Google OAuth2.0 to Specific Domain Name