简体   繁体   English

无法使用 Ruby on Rails、Devise、Omniauth 在 Google 和玩具网站之间设置简单的 OAuth2

[英]Can't setup simple OAuth2 between Google and toy website using Ruby on Rails, Devise, Omniauth

I am trying to learn Ruby on Rails by building a small web application.我正在尝试通过构建一个小型 web 应用程序来学习 Ruby on Rails。 My first step was to start with OAuth login so users could login using Facebook, Google etc. But when I go to the /users/sign_up devise page on my localhost and click on Sign in with GoogleOauth2, it "does nothing" and the console tells me:我的第一步是从 OAuth 登录开始,这样用户就可以使用 Facebook、Google 等登录。但是当我 go 到我本地主机上的/users/sign_up devise 页面并单击使用 GoogleOauth2 登录时,它“什么都不做”并且控制台告诉我:

D, [2021-10-05T04:55:04.716439 #10144] DEBUG -- omniauth: (google_oauth2) Request phase initiated.
W, [2021-10-05T04:55:04.730086 #10144]  WARN -- omniauth: Attack prevented by OmniAuth::AuthenticityTokenProtection
E, [2021-10-05T04:55:04.730681 #10144] ERROR -- omniauth: (google_oauth2) Authentication failure! authenticity_error: OmniAuth::AuthenticityError, Forbidden

I've setup devise and omniauth-google-oauth2, and registered an app in the Google developer console, adding the appropriate callback uris as http://127.0.0.1:3000/users/auth/google_oauth2/callback and http://localhost:3000/users/auth/google_oauth2/callback , and wrote the key and secret to.env using the dotenv gem to read them, running the server with dotenv rails server .我已经设置了 devise 和 omniauth-google-oauth2,并在 Google 开发者控制台中注册了一个应用程序,将适当的回调 uris 添加为http://127.0.0.1:3000/users/auth/google_oauth2/callbackhttp://localhost:3000/users/auth/google_oauth2/callback ,并使用 dotenv gem 将密钥和秘密写入 .env 以读取它们,使用dotenv rails server运行服务器。

I'd like to understand what is wrong here, why, how would I go about debugging this, and how to fix it so that logging in through Google takes me to my homepage, the "Yay. You're on rails!"我想了解这里出了什么问题,为什么,我 go 如何调试它,以及如何修复它以便通过 Google 登录将我带到我的主页,“耶。你在轨道上!” screen.屏幕。

My files are setup as such:我的文件设置如下:

routes.rb:路线.rb:

Rails.application.routes.draw do
  devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth' }
end

app/models/users.rb:应用程序/模型/users.rb:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise  :database_authenticatable, :registerable,
        :recoverable, :rememberable, :validatable,
         :timeoutable,
        :omniauthable, omniauth_providers: [:google_oauth2]

  def self.create_from_google_data(provider_data)
    where(provider: provider_data.provider, uid: provider_data.uid).first_or_create do | user |
      user.email = provider_data.info.email
      user.password = Devise.friendly_token[0, 20]
      user.skip_confirmation!
    end
  end
end

app/config/initializers/devise.rb:应用程序/配置/初始化程序/devise.rb:

...
config.omniauth :google_oauth2, ENV['GOOGLE_APP_ID'], ENV['GOOGLE_APP_SECRET'], scope: 'userinfo.email,userinfo.profile'
...

app/controllers/users/omniauth_controller.rb应用程序/控制器/用户/omniauth_controller.rb

class Users::OmniauthController < ApplicationController
    def google_oauth2
        @user = User.create_from_google_data(request.env['omniauth.auth'])
        if @user.persisted?
          sign_in_and_redirect @user
          set_flash_message(:notice, :success, kind: 'Google') if is_navigational_format?
        else
          flash[:error] = 'There was a problem signing you in through Google. Please register or try signing in later.'
          redirect_to new_user_registration_url
        end 
    end
    def failure
        flash[:error] = 'There was a problem signing you in. Please register or try signing in later.' 
        redirect_to new_user_registration_url
    end
end

app/config/initializers/session_store.rb:应用程序/配置/初始化程序/session_store.rb:

Rails.application.config.session_store :active_record_store, key: '_devise-omniauth_session'

Please let me know if any further clarification is needed to debug this issue.如果需要任何进一步的说明来调试此问题,请告诉我。

For anyone stumbling upon the same issue, I solved it by adding the following gem to the project, after trying tons of fixes I found online: gem "omniauth-rails_csrf_protection" Why?对于遇到相同问题的任何人,在尝试了我在网上找到的大量修复程序后,我通过将以下 gem 添加到项目中来解决它: gem "omniauth-rails_csrf_protection"为什么? No clear idea.没有明确的想法。 Answers with more explanation would be very welcome.非常欢迎提供更多解释的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM