繁体   English   中英

request.env['omniauth.auth'] 越来越零

[英]request.env['omniauth.auth'] is getting nil

所以我试图在 Ruby on rails 的 Active Admin 中设置 Google Omniauth2 身份验证,当用户在 google 注册对话框中点击他的帐户后,它被重定向到root_path ie /

这是我在响应或请求中收到的错误

Started POST "/admin/auth/google_oauth2" for ::1 at 2022-02-20 02:07:04 +0530
D, [2022-02-20T02:07:04.882189 #137717] DEBUG -- omniauth: (google_oauth2) Request phase initiated.
Started GET "/admin/auth/google_oauth2/callback?state=e3435afeb73ff096cdf7c2fba4e9dff0c009f2d581b282f8&code=4%2F0AX4XfWg19pJcL4XrQkseJrAEYZawewR80PHY5oNFHBAbmXSNzhzy_HoMcFlRaE4UR6HiOw&scope=email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=consent" for ::1 at 2022-02-20 02:07:08 +0530
D, [2022-02-20T02:07:08.048851 #137717] DEBUG -- omniauth: (google_oauth2) Callback phase initiated.
E, [2022-02-20T02:07:08.203534 #137717] ERROR -- omniauth: (google_oauth2) Authentication failure! undefined method `bytesize' for {"client_id"=>"7345634785-5b43jhb53hb34jhbr43hjrb318egf1.apps.googleusercontent.com", "client_secret"=>"XXXXXX-9oO4eKfhghdfjghfddfk-wfA", "grant_type"=>"authorization_code", "code"=>"4/0AX4XfWg19pJcL4XrQkseJrAEYZawewR80PHY5oNFHBAbmXSNzhzy_HoMcFlRaE4UR6HiOw", :redirect_uri=>"http://localhost:3000/admin/auth/google_oauth2/callback"}:Hash: NoMethodError, undefined method `bytesize' for {"client_id"=>"7345634785-5b43jhb53hb34jhbr43hjrb318egf1.apps.googleusercontent.com", "client_secret"=>"XXXXXX-9oO4eKfhghdfjghfddfk-wfA", "grant_type"=>"authorization_code", "code"=>"4/0AX4XfWg19pJcL4XrQkseJrAEYZawewR80PHY5oNFHBAbmXSNzhzy_HoMcFlRaE4UR6HiOw", :redirect_uri=>"http://localhost:3000/admin/auth/google_oauth2/callback"}:Hash
Processing by AdminUsers::OmniauthCallbacksController#failure as HTML
  Parameters: {"state"=>"e3435afeb73ff096cdf7c2fba4e9dff0c009f2d581b282f8", "code"=>"4/0AX4XfWg19pJcL4XrQkseJrAEYZawewR80PHY5oNFHBAbmXSNzhzy_HoMcFlRaE4UR6HiOw", "scope"=>"email https://www.googleapis.com/auth/userinfo.email openid", "authuser"=>"0", "prompt"=>"consent"}
Redirected to http://localhost:3000/
Completed 302 Found in 1ms (ActiveRecord: 0.0ms | Allocations: 352)

此控件返回故障地址后

配置/初始化程序/devise.rb

config.omniauth :google_oauth2,
      Rails.application.credentials.google[:client_id],
      Rails.application.credentials.google[:secret_key],
      scope: 'email',

应用程序/模型/admin_user.rb

class AdminUser < ApplicationRecord
  devise :database_authenticatable, 
         :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2]
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0, 20]
      user.name = auth.info.name
      user.image = auth.info.image
    end
  end

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.google_data"] && session["devise.google_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end
end

应用程序/控制器/管理员/admin_users

class AdminUsers::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  # See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
  # skip_before_action :verify_authenticity_token, only: :google_oauth2

  def google_oauth2
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @admin_user = AdminUser.from_omniauth(request.env['omniauth.auth'])
    debugger.info "google_oauth2"
    if @admin_user.persisted?
      sign_in @admin_user#, event: :authentication # this will throw if @user is not activated
      set_flash_message(:notice, :success, kind: 'Google') if is_navigational_format?
    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_path
    end
  end

  def failure
    flash[:error] = 'There was a problem signing you in. Please register or try signing in later.'
    redirect_to root_path
  end
end

我在 faliure 中得到 request.env['omniauth.auth'] nil 而在 google_oath2 方法中没有得到任何 output 这意味着我的控件不会进入 google_oath2 回调,而是进入 faliure

问题出在 oauth_2 默认版本

gem 'oauth2', '~> 1.4.9'

将它添加到我的 gem 文件中,然后运行bundle install解决了这个问题

暂无
暂无

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

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