繁体   English   中英

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

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

我正在尝试通过构建一个小型 web 应用程序来学习 Ruby on Rails。 我的第一步是从 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

我已经设置了 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运行服务器。

我想了解这里出了什么问题,为什么,我 go 如何调试它,以及如何修复它以便通过 Google 登录将我带到我的主页,“耶。你在轨道上!” 屏幕。

我的文件设置如下:

路线.rb:

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

应用程序/模型/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

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

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

应用程序/控制器/用户/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

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

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

如果需要任何进一步的说明来调试此问题,请告诉我。

对于遇到相同问题的任何人,在尝试了我在网上找到的大量修复程序后,我通过将以下 gem 添加到项目中来解决它: gem "omniauth-rails_csrf_protection"为什么? 没有明确的想法。 非常欢迎提供更多解释的答案。

暂无
暂无

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

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