簡體   English   中英

設計Omniauth-facebook旁路Rails驗證

[英]Devise Omniauth-facebook bypass rails validations

我剛剛按照這篇文章在我的Rails 4應用程序中實現omniauth。

在我的用戶模型中,我有兩種類型的用戶,即ADMIN(由表單創建)和MEMBER(由omniauth創建)。 我需要繞過將由omniauth創建的MEMBER用戶的所有驗證。

我知道可以通過傳遞此選項來完成:

save(validate: false)

但是,采用了first_or_create方法:

  def self.from_omniauth(auth)
    # where(auth.slice(:provider, :uid)).first_or_create do |user|
    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   # assuming the user model has a name
      user.image = auth.info.image # assuming the user model has an image
    end
  end

因此,我的問題是:在這種omniauth情況下,如何繞過所有驗證?

請指教。

first_or_create似乎沒有任何選擇,但是您仍然有幾個選擇。

除了first_or_create ,您可以執行first_or_initialize ,它的作用與first_or_create相同,但是不會保存。 然后,您將擁有一條新記錄,可以在其上調用save(validate: false)

但是,那么您在系統中會有無效的記錄,因此,例如,如果您要更新這些記錄的電子郵件之一,則必須重新進行驗證。

我建議將這種邏輯轉移到驗證本身:

validate_presence_of :name, unless: -> { from_omniauth? }

private

def from_omniauth?
  provider && uid
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM