簡體   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