Rails - Google oauth2 request.env['omniauth.auth'] is nil using omniauth with multiple models

James N

I am working on a rails app and setting up Google OAuth2 using the omniauth google oauth2 gem. There are 2 models that are using devise and omniauth, and following this guide for using devise with multiple models (From the devise team) does not work.

Currently, there is 1 existing model using devise which is using an omniauth strategy for facebook. The set up for that model looks like this

devise :invitable, :database_authenticatable, :registerable,
         :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook, :facebook_access_token]

On a seperate model, I want to add an omniauth strategy for google (that model is also using devise) which has the standard devise set up

devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

currently in devise.rb there is set up for facebook omniauth

config.omniauth :facebook, ENV['facebook_app_id'], ENV['facebook_app_secret'], { scope: 'comma,seperated,fields', info_fields: 'comma,seperated,fields' }

and in omniauth.rb there is set up for google

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

in routes.rb there are various routes for devise/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'

Note - there is no controllers: {omniauth_callbacks: "admin/omniauth_callbacks"} for admin_users in routes.rb because I get this error booting up my server

Please add `devise :omniauthable` to the `AdminUser` model

and if omniauthable is added to both models, I get this error when running my local server

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

The current set up in the initialization files and routes.rb is allowing the server to turn on. Because I cannot use this the way the guide has shown, I am using the 1-time hybrid auth flow outlined in the omniauth-google-oauth2 gem guide. In the page rendering that view, I have the javascript set up like this

<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>

and I have added that route like this

namespace :admin do 
  ...
  post '/admin-signin', to: 'application#google_oauth2'
end

Currently, this set up lets me render the form prompt from google, I can select my account to use. Once I select the account to use, I can see the params being sent to the controller action and it looks like this

{"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"}

My controller action to handle this call looks like this

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

But the problem is request.env['omniauth.auth'] is nil and no data is being sent with it. It looks like i might have a couple options

  • is there something additional I need to do on my end to get request.env['omniauth.auth'] to be present? Is there a callback I send somewhere else to google to get the request.env['omniauth.auth'] data back?
  • Implement an omniauthable model that both users and admin_users can use

Does anyone see what I'm doing wrong, or what can be added to this? All I want from the google oauth is this auth hash that should be present in request.env['omniauth.auth'].

Thanks for the help

James N

This was a very specialized and isolated issue that might not be too much help to someone else running across this. I made this app inside of an organization that had various google keys. So the solution for this problem for me was to use the proper keys. The keys I used before did not satisfy the settings of the organization and it was not working as a result. Once I used the proper keys for that organization, this was able to work just fine.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails-在多个模型上使用omniauth时,Google oauth2 request.env ['omniauth.auth']为零

来自分类Dev

request.env ['omniauth.auth']在Ruby on Rails中为零

来自分类Dev

Google omniauth + devise + @域访问+ Rails

来自分类Dev

JSON参数在请求哈希中不可用(Rails,omniauth-google-oauth2 gem)

来自分类Dev

OAuth2和Omniauth之间的区别

来自分类Dev

OAuth2和Omniauth之间的区别

来自分类Dev

Rails,用于松弛的Omniauth:如何访问Auth Hash信息

来自分类Dev

redirect_uri_mismatch与Heroku上的OmniAuth Google oauth2

来自分类Dev

带有devise和omniauth的Google oauth2被视为失败

来自分类Dev

Rails设计专家omniauth

来自分类Dev

Omniauth_google_oauth2错误:redirect_uri_mismatch

来自分类Dev

在Rails中设置omniauth后访问Google Calendar API

来自分类Dev

Rails ActiveModel :: ForbiddenAttributesError设计,Omniauth

来自分类Dev

Rails 5,Devise,Omniauth,Twitter

来自分类Dev

Rails 4.1.5 omniauth strong parameters

来自分类Dev

Rails omniauth不刷新数据

来自分类Dev

Rails 4.1.5 omniauth强参数

来自分类Dev

Rails中的Omniauth / MYSQL尴尬

来自分类Dev

Rails omniauth不刷新数据

来自分类Dev

Rails 4 OmniAuth Facebook NoAuthorizationErrorCode

来自分类Dev

Rails API Omniauth(Facebook)登录

来自分类Dev

Google omniauth无法识别路径

来自分类Dev

Google omniauth无法识别路径

来自分类Dev

使用omniauth-google-oauth2的Google Oauth登录经常失败

来自分类Dev

Rails 4-带有omniauth-google-oauth2的Google一次性代码流,不断获取“无效代码”

来自分类Dev

在Rails应用程序中使用Omniauth-oauth2刷新令牌

来自分类Dev

如何配置使用omniauth-google-oauth2返回的图片大小?

来自分类Dev

如何在Rails 4中保存Facebook omniauth的request.referrer

来自分类Dev

Rails中的omniauth-facebook验证错误

Related 相关文章

  1. 1

    Rails-在多个模型上使用omniauth时,Google oauth2 request.env ['omniauth.auth']为零

  2. 2

    request.env ['omniauth.auth']在Ruby on Rails中为零

  3. 3

    Google omniauth + devise + @域访问+ Rails

  4. 4

    JSON参数在请求哈希中不可用(Rails,omniauth-google-oauth2 gem)

  5. 5

    OAuth2和Omniauth之间的区别

  6. 6

    OAuth2和Omniauth之间的区别

  7. 7

    Rails,用于松弛的Omniauth:如何访问Auth Hash信息

  8. 8

    redirect_uri_mismatch与Heroku上的OmniAuth Google oauth2

  9. 9

    带有devise和omniauth的Google oauth2被视为失败

  10. 10

    Rails设计专家omniauth

  11. 11

    Omniauth_google_oauth2错误:redirect_uri_mismatch

  12. 12

    在Rails中设置omniauth后访问Google Calendar API

  13. 13

    Rails ActiveModel :: ForbiddenAttributesError设计,Omniauth

  14. 14

    Rails 5,Devise,Omniauth,Twitter

  15. 15

    Rails 4.1.5 omniauth strong parameters

  16. 16

    Rails omniauth不刷新数据

  17. 17

    Rails 4.1.5 omniauth强参数

  18. 18

    Rails中的Omniauth / MYSQL尴尬

  19. 19

    Rails omniauth不刷新数据

  20. 20

    Rails 4 OmniAuth Facebook NoAuthorizationErrorCode

  21. 21

    Rails API Omniauth(Facebook)登录

  22. 22

    Google omniauth无法识别路径

  23. 23

    Google omniauth无法识别路径

  24. 24

    使用omniauth-google-oauth2的Google Oauth登录经常失败

  25. 25

    Rails 4-带有omniauth-google-oauth2的Google一次性代码流,不断获取“无效代码”

  26. 26

    在Rails应用程序中使用Omniauth-oauth2刷新令牌

  27. 27

    如何配置使用omniauth-google-oauth2返回的图片大小?

  28. 28

    如何在Rails 4中保存Facebook omniauth的request.referrer

  29. 29

    Rails中的omniauth-facebook验证错误

热门标签

归档