我正在尝试通过构建一个小型 Web 应用程序来学习 Ruby on Rails。我的第一步是从 OAuth 登录开始,以便用户可以使用 Facebook、Google 等登录。但是当我转到/users/sign_up
本地主机上的设计页面并单击使用 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/callback
和http://localhost:3000/users/auth/google_oauth2/callback
,并使用 dotenv gem 将密钥和秘密写入 .env 以读取它们,运行服务器与dotenv rails server
.
我想了解这里出了什么问题,为什么,我将如何调试它,以及如何修复它,以便通过谷歌登录将我带到我的主页,“耶!你在轨道上!” 屏幕。
我的文件是这样设置的:
路线.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'
请让我知道是否需要进一步澄清来调试此问题。