【发布时间】:2020-01-01 02:24:00
【问题描述】:
我正在开发一个 Rails 应用程序并使用 omniauth google oauth2 gem 设置 Google OAuth2。有 2 个模型正在使用 devise 和 omniauth,并且关注 this guide 将 devise 与多个模型一起使用(来自设计团队)不起作用。
目前,有 1 个使用 devise 的现有模型,它正在为 facebook 使用omniauth 策略。该模型的设置如下所示
devise :invitable, :database_authenticatable, :registerable,
:trackable, :validatable, :omniauthable, omniauth_providers: [:facebook, :facebook_access_token]
在一个单独的模型上,我想为谷歌添加一个omniauth策略(该模型也使用devise),它设置了标准设计
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
目前在devise.rb 中设置了 facebook omniauth
config.omniauth :facebook, ENV['facebook_app_id'], ENV['facebook_app_secret'], { scope: 'comma,seperated,fields', info_fields: 'comma,seperated,fields' }
在omniauth.rb 中设置了谷歌
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'],
{
scope: 'userinfo.email, userinfo.profile',
prompt: 'select_account',
image_aspect_ratio: 'square',
image_size: 50
}
end
在routes.rb 中有多种途径用于设计/omniauth
devise_for :users, path: 'users', controllers: {omniauth_callbacks: "users/omniauth_callbacks"} do
get 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session
end
devise_for :admin_users, path: 'admin/admin_users'
注意 - routes.rb 中没有 admin_users 的 controllers: {omniauth_callbacks: "admin/omniauth_callbacks"},因为我在启动服务器时遇到此错误
Please add `devise :omniauthable` to the `AdminUser` model
如果两个模型都添加了omniauthable,我在运行本地服务器时会收到此错误
Wrong OmniAuth configuration. If you are getting this exception, it means that either:
1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one
2) You are setting :omniauthable in more than one model
3) You changed your Devise routes/OmniAuth setting and haven't restarted your server
初始化文件和 routes.rb 中的当前设置允许服务器开启。因为我不能按照指南显示的方式使用它,所以我使用了omniauth-google-oauth2 gem 指南中概述的1-time hybrid auth flow。在呈现该视图的页面中,我将 javascript 设置为这样
<script src="https://apis.google.com/js/platform.js?onload=init"></script>
<script type="text/javascript">
function init() {
gapi.load('auth2', function() {
// Ready.
$('.google-login-button').click(function(e) {
e.preventDefault();
gapi.auth2.authorize({
client_id: "SOMEKEY.apps.googleusercontent.com",
cookie_policy: 'single_host_origin',
scope: 'email profile',
response_type: 'code'
}, function(response) {
if (response && !response.error) {
// google authentication succeed, now post data to server.
jQuery.ajax({type: 'POST', url: '/admin/admin-signin', data: response,
success: function(data) {
// response from server
console.log('********************')
console.log(data)
console.log('********************')
}
});
} else {
// google authentication failed
console.log('********************')
console.log(response)
console.log('********************')
}
});
});
});
};
init();
</script>
<div class="authform">
<a class="google-login-button">Sign in</a>
</div>
我已经像这样添加了这条路线
namespace :admin do
...
post '/admin-signin', to: 'application#google_oauth2'
end
目前,此设置允许我从 google 呈现表单提示,我可以选择要使用的帐户。选择要使用的帐户后,我可以看到正在发送到控制器操作的参数,它看起来像这样
{"code"=>"4/LONGCODEOFSTRINGSANDNUMBERS",
"scope"=>"email profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
"authuser"=>"1", "hd"=>"COMPANY", "session_state"=>"b0c030e5f1304f58695e19aa29cb73c942ac69b6..163d",
"prompt"=>"consent", "controller"=>"admin/application", "action"=>"google_oauth2"}
我处理此调用的控制器操作如下所示
def google_oauth2
@admin_user = AdminUser.from_omniauth(request.env['omniauth.auth'])
if @admin_user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
sign_in_and_redirect @admin_user, event: :authentication
else
session['devise.google_data'] = request.env['omniauth.auth'].except(:extra)
redirect_to :back, alert: @admin_user.errors.full_messages.join("\n")
end
end
但问题是request.env['omniauth.auth'] 是 nil 并且没有数据随它一起发送。看来我可能有几个选择
- 我需要做些什么才能让
request.env['omniauth.auth']出席吗?是否有我向其他地方发送回调到 google 以取回request.env['omniauth.auth']数据? - 实现用户和管理员用户都可以使用的
omniauthable模型
有没有人看到我做错了什么,或者可以添加什么?我想从 google oauth 得到的只是 this auth hash,它应该出现在 request.env['omniauth.auth'] 中。
感谢您的帮助
【问题讨论】:
标签: ruby-on-rails google-oauth