我正在尝试使用Rails 5.2.1应用和gem'omniauth-google-oauth2'设置google登录。我期望authenticate_user将为我创建用户实例,但它没有考虑我在Users :: OmniauthCallbacksController中编写的任何代码。我可以看到google登录页面,单击“允许”后成功调用已配置的网址。只是current_user尚未设置。我将不胜感激任何调试指导或文章指导我。
我尝试将binding.pry放在warden-1.2.7 / lib / warden / proxy.rb中的wardens def authenticate!(* args)中,但似乎只有一种策略:{:user => []} 。谷歌oauth也应该作为一种策略吗?
routes.rb:
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
gemfile:
gem 'rails', '~> 5.2.1'
gem 'devise'
gem 'omniauth-google-oauth2'
devise.rb: MISTAKES是我正在创建的应用的名称
require 'omniauth-google-oauth2'
GOOGLE_OAUTH_MISTAKES_CONFIGURATION = {
scope: "contacts.readonly,userinfo.email",
redirect_uri: 'http://localhost:3000/home/index'
}
config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], GOOGLE_OAUTH_MISTAKES_CONFIGURATION
OmniauthCallbacksController
def google_oauth2
binding.pry # Control does not reach here
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
sign_in_and_redirect @user, event: :authentication
else
session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) # Removing extra as it can overflow some session stores
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
user.rb:
devise :omniauthable, omniauth_providers: [:google_oauth2]
.
.
.
def self.from_omniauth(access_token)
binding.pry # Control does not reach here
data = access_token.info
user = User.where(email: data['email']).first
# Uncomment the section below if you want users to be created if they don't exist
# unless user
# user = User.create(name: data['name'],
# email: data['email'],
# password: Devise.friendly_token[0,20]
# )
# end
user
end
我希望在Google重定向到我配置的redirect_uri(它是)之后,before_action:authenticate_user!将为我提供current_user的实例,但它只是抛出401次很多,直到由于太多401而退出浏览器