2014-11-02 75 views
3

我已經花了最後一天的時間來解決這個問題,這使我瘋了。Rails devise omniauth-facebook .persisted?返回false

昨晚,我在我的網站上登錄了Facebook登錄,並且正在檢索基本的用戶信息。當我加入:scope => 'user_birthday'config/initializers/omniauth.rb現在看起來是這樣的

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, "APP_ID", "APP_SECRET", :scope => 'user_birthday' 
end 

僅供參考,我從config/initializers/devise.rb

刪除線config.omniauth :facebook, "APP_ID", "APP_SECRET"我的問題開始我花了幾個小時試圖讓這個工作,但不得不放棄最終。然後今天早上我再次運行它,它工作。喜出望外,我試圖給:scope添加另一個參數,但現在整個事情又被打破了。如果我刪除了:scope,但每當我把它重新放入它時(即使它只是:scope => 'user_birthday',就像我今天早上開始工作的第一件東西),我可以使它工作。

要查找的問題,我把調試代碼omniauth_callbacks_controller.rb,它現在的樣子:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    # You need to implement the method below in your model (e.g. app/models/user.rb) 
     @user = User.from_omniauth(request.env["omniauth.auth"]) 
     puts "start before persist debug" 
     puts @user.birthday 
     puts @user.persisted? 
     puts "end before persist debug" 

    if @user.persisted?   
      puts "Start persisted debug" 
      puts request.env["omniauth.auth"] 
      puts "End debug"    
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
      puts "Start unpersisted debug" 
      puts request.env["omniauth.auth"] 
      puts "End debug" 
     redirect_to new_user_registration_url 
    end 
    end 
end 

該調試清楚地表明,Facebook正在返回所需的信息,但該應用程序失敗,因爲.persisted?是返回false和所以我得到重新定向到返回下面的new_user_registration頁: -

NoMethodError in Devise::Registrations#new 
Showing /home/action/workspace/cloudapp/app/views/devise/shared/_links.html.erb where line #23 raised: 

undefined method `omniauth_authorize_path' for #<#<Class:0x007f3aeffff038>:0x007f3aefffdf08> 

我不能爲我的生命弄清楚爲什麼.persisted?是返回false。我用Heroku postgresql數據庫使用Nitrous.io進行開發。我已經證實有數據庫中沒有用戶通過運行

rails c 
User.all 

這將返回:

User Load (89.4ms) SELECT "users".* FROM "users"                                                  
=> #<ActiveRecord::Relation []> 

我有一種感覺,問題是出在models/user.rb但我無法弄清楚如何調試它看看它是否找到了一個用戶,因此不會堅持或試圖創建一個和失敗。有誰知道一個簡單的方法來調試呢?

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 # assuming the user model has a name 
     user.birthday = auth.extra.raw_info.birthday 
     # user.image = auth.info.image # assuming the user model has an image 
    end 
end 

我已經完成了大約50次,接近放棄。

我能想到的唯一的事情是where(provider: auth.provider, uid: auth.uid)返回的東西(這不應該是因爲我的數據庫是空的)。是否可能有一個索引存在於我的數據庫之外的某個地方,這就是它正在搜索的內容?

請爲我的理智,任何人都可以幫忙嗎?如果您需要更多的信息,我會很樂意提供它

Edit 1

剛試過以下,它的作品,這使得我更加糊塗比以往任何時候:

  1. 從我的Facebook帳戶刪除該應用程序爲我使用該帳戶進行測試
  2. 嘗試使用臉書登錄並使用:scope => 'user_birthday'離開。Facebook列出了所需的權限爲your public profile and birthday。接受並返回到我的網站,失敗按上述(即使信息肯定被髮回)
  3. 刪除:scope => 'user_birthday'並嘗試再次登錄Facebook使用。定向到Facebook,其中列出了要求爲your public profile and email address的權限。接受並獲得指導回到現在有效的網站,並且還有用戶的生日存儲和訪問,因爲我從上面的第2號的facebook獲得了許可。

我完全不知所措現在

+0

什麼是你的生日列的類型(用戶表)? – mohameddiaa27 2014-11-03 00:19:28

+0

字符串。根據我的編輯,它是保存和訪問步驟3 – 2014-11-03 00:24:07

回答

3

要了解的對象沒有被保存原因。你需要把錯誤。

puts @user.errors.to_a 

,並檢查的auth

puts request.env["omniauth.auth"] 
+1

非常感謝這個mohameddiaa27。我不知道如何調試.first_or_create(),但是.errors.to_a()完全可以。問題是當我要求user_birthday時,facebook停止發送用戶電子郵件出於某種原因,並引發錯誤'電子郵件不能爲空'。如果我還要求user_birthday,我想我必須明確地將其添加到範圍。再次感謝。 – 2014-11-03 09:39:26

0

我有同樣的問題的內容,然後按照上面的答案,我把「@ user.errors.to_yaml」在我的代碼,我看到那裏是我發現的錯誤。

我也使用「devise」和「omniauth-facebook」。 omn​​iauth-facebook的默認範圍是「email」。然而,我把屬性的範圍:「user_about_me,user_status,user_location,user_birthday,user_photos」。我需要在範圍中添加「EMAIL」以設計用於創建'用戶'。當我看到我的錯誤時,我發現了這一點:「電子郵件不是空白的」。

摘要: 如果您在範圍上插入屬性,請始終輸入「email」。

0

的Facebook並不總是用戶

從Facebook開發https://developers.facebook.com/bugs/298946933534016

一些可能的原因返回電子郵件:

  • 的帳戶沒有電子郵件地址
  • 沒有在賬戶確認的電子郵件地址
  • 帳戶上沒有經過驗證的電子郵件地址
  • 用戶進入這需要他們重新確認 他們的電子郵件地址的安全檢查站,他們還沒有這樣做
  • 用戶的電子郵件地址是不可達

您還需要「電子郵件」擴展權限,即使用戶誰有一個有效的,確認的,可到達的電子郵件地址的文件。

用戶進入這需要他們重新確認他們的電子郵件地址的安全檢查站,他們還沒有這樣做 用戶的電子郵件地址是不可達

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
     puts request.env["omniauth.auth"] # check if request.env["omniauth.auth"] is provided an email 
     if request.env["omniauth.auth"].info.email.present? 
     @user = User.from_omniauth(request.env["omniauth.auth"]) 
      if @user.persisted?     
       sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
       set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
      else 
       session["devise.facebook_data"] = request.env["omniauth.auth"] 
       redirect_to new_user_registration_url 
      end 
     else 
      redirect_to new_user_registration_url, notice: "Can't sign in to your #{request.env["omniauth.auth"].provider} account. Try to sign up." 
     end 
    end 
    end