ruby ActiveRecord::RecordInvalid(验证失败:Uid不能为空)Omniauth LinkedIn Devise

7gcisfzg  于 2024-01-07  发布在  Ruby
关注(0)|答案(1)|浏览(165)

在保存User模型时,我收到(验证失败:Uid不能为空)。Uid本身属于Identity对象-它作为多个身份类型的单个身份访问点。换句话说,您将能够使用facebook,twitter,或linkedin omniauth策略在应用程序中,而无需创建多个帐户。它似乎并不像广告中所说的那样工作。鉴于我无法创建用户在应用程序上登录领英后。
我尝试将env引用替换为request. env。因为,它在其当前作用域中不存在。我尝试查找错误,但无法准确找到身份模型在创建用户之前未存储身份的原因。它在创建User对象后立即失败,而不是检查User是否存在,是否附加了身份或应创建身份。
服务器开发日志

Started GET "/users/auth/linkedin" for 127.0.0.1 at 2019-08-03 22:56:03 -0400
I, [2019-08-03T22:56:04.789247 #23148]  INFO -- omniauth: (linkedin) Request phase initiated.
Started GET "/users/auth/linkedin/callback?oauth_token=77--2555555-5555-4444-ssfs-dcsfsfsfsfsf93&oauth_verifier=28090" for 127.0.0.1 at 2019-08-03 22:56:31 -0400
I, [2019-08-03T22:56:32.422234 #23148]  INFO -- omniauth: (linkedin) Callback phase initiated.
Processing by OmniauthCallbacksController#linkedin as HTML
  Parameters: {"oauth_token"=>"77--2555555-5555-4444-ssfs-dcsfsfsfsfsf93", "oauth_verifier"=>"28090"}
  Identity Load (1.4ms)  SELECT  "identities".* FROM "identities" WHERE "identities"."uid" IS NULL AND "identities"."provider" = ? LIMIT ?  [["provider", "linkedin"], ["LIMIT", 1]]
  ↳ app/models/identity.rb:7
   (0.2ms)  begin transaction
  ↳ app/models/identity.rb:7
  Identity Exists (1.1ms)  SELECT  1 AS one FROM "identities" WHERE "identities"."uid" IS NULL AND "identities"."provider" = ? LIMIT ?  [["provider", "linkedin"], ["LIMIT", 1]]
  ↳ app/models/identity.rb:7
   (0.2ms)  rollback transaction
  ↳ app/models/identity.rb:7
   (0.2ms)  begin transaction
  ↳ app/models/user.rb:47
  User Exists (2.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "[email protected]"], ["LIMIT", 1]]
  ↳ app/models/user.rb:47
  User Create (44.3ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "confirmed_at") VALUES (?, ?, ?, ?, ?)  [["email", "[email protected]"], ["encrypted_password", "$2a"], ["created_at", "2019-08-04 02:56:33.928307"], ["updated_at", "2019-08-04 02:56:33.928307"], ["confirmed_at", "2019-08-04 02:56:33.921311"]]
  ↳ app/models/user.rb:47
   (134.0ms)  commit transaction
  ↳ app/models/user.rb:47
   (0.1ms)  begin transaction
  ↳ app/models/user.rb:54
  Identity Exists (2.1ms)  SELECT  1 AS one FROM "identities" WHERE "identities"."uid" IS NULL AND "identities"."provider" = ? LIMIT ?  [["provider", "linkedin"], ["LIMIT", 1]]
  ↳ app/models/user.rb:54
   (0.2ms)  rollback transaction
  ↳ app/models/user.rb:54
Completed 422 Unprocessable Entity in 879ms (ActiveRecord: 191.3ms)


ActiveRecord::RecordInvalid (Validation failed: Uid can't be blank):

app/models/user.rb:54:in `find_for_oauth'
(eval):3:in `linkedin'

字符串
omniauth_callbacks_controller.rb

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def self.provides_callback_for(provider)
    class_eval %Q{
      def #{provider}
        @user = User.find_for_oauth(request.env["omniauth.auth"], current_user)

        if @user.persisted?
          sign_in_and_redirect @user, event: :authentication
          set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
        else
          session["devise.#{provider}_data"] = env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
    }
  end

  [:facebook, :linkedin].each do |provider|
    provides_callback_for provider
  end

  def after_sign_in_path_for(resource)
    if resource.email_verified?
      super resource
    else
      finish_signup_path(resource)
    end
  end

  def failure
    redirect_to root_path
  end
end


identity.rb

class Identity < ApplicationRecord
  belongs_to :user
  validates_presence_of :uid, :provider
  validates_uniqueness_of :uid, :scope => :provider

  def self.find_for_oauth(auth)
    find_or_create_by(uid: auth.uid, provider: auth.provider)
  end
end


user.rb

class User < ApplicationRecord
  TEMP_EMAIL_PREFIX = 'change@me'
  TEMP_EMAIL_REGEX = /\Achange@me/

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :omniauthable, :trackable, :confirmable

  validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update

  def self.find_for_oauth(auth, signed_in_resource = nil)

    # Get the identity and user if they exist
    identity = Identity.find_for_oauth(auth)

    # If a signed_in_resource is provided it always overrides the existing user
    # to prevent the identity being locked with accidentally created accounts.
    # Note that this may leave zombie accounts (with no associated identity) which
    # can be cleaned up at a later date.
    user = if signed_in_resource then
             signed_in_resource
           else
             identity.user
           end

    # Create the user if needed
    if user.nil?

      # Get the existing user by email if the provider gives us a verified email.
      # If no verified email was provided we assign a temporary email and ask the
      # user to verify it on the next step via UsersController.finish_signup
      email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
      email = auth.info.email if email_is_verified
      user = User.where(:email => email).first if email

      # Create the user if it's a new registration
      if user.nil?
        user = User.new(
            name: auth.extra.raw_info.name,
            #username: auth.info.nickname || auth.uid,
            email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
            password: Devise.friendly_token[0,20]
        )
        user.skip_confirmation!
        user.save!
      end
    end

    # Associate the identity with the user if needed
    if identity.user != user
      identity.user = user  
      identity.save!     # Where error occurs
    end
    user
  end

  def email_verified?
    self.email && self.email !~ TEMP_EMAIL_REGEX
  end

  protected
  def confirmation_required?
    false
  end
end


预期行为:用户将成功登录其Linkedin帐户,并被重定向到应用程序以完成注册。
实际结果:用户对象创建后,无法保存Identity,并将Uid留空。

4uqofj5v

4uqofj5v1#

这个错误是一个验证错误,UID为空,这就是问题所在。所以看起来你已经有一个UID为空的用户记录保存在DB中。你的验证不允许你添加其他UID为空的用户。所以不知何故,你的UID没有正确通过,所以你的验证将失败。- lacostenycoder 2019年8月4日5:29

相关问题