2011-07-10 28 views
8

我正在使用Omnisuth設計讓用戶使用Facebook登錄我的應用程序。我使用Railscast教程來啓動並運行它。爲新用戶設計Omniauth「encrypted_pa​​ssword不能爲NULL」

如果用戶已經是我的網站的成員通過臉書認證工作正常。使用Facebook驗證新用戶時出現問題。當它爲我的用戶模型創建一個新用戶時,我得到「users.encrypted_pa​​ssword可能不是NULL」錯誤。我無法弄清楚如何通過Facebook信息將密碼傳遞給用戶模型。

這是我有:

authentations_controller.rb

class AuthenticationsController < ApplicationController 
def index 
    @authentications = current_user.authentications if current_user 
end 

def create 
    omniauth = request.env["omniauth.auth"] 
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 
if authentication 
    flash[:notice] = "Signed in successfully." 
    sign_in_and_redirect(:user, authentication.user) 
elsif current_user 
    current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid']) 
    flash[:notice] = "Authentication successful." 
    redirect_to authentications_url 
else 
    user = User.new 
    user.apply_omniauth(omniauth) 
    if user.save 
    flash[:notice] = "Signed in successfully." 
    sign_in_and_redirect(:user, user) 
    else 
    session[:omniauth] = omniauth.except('extra') 
    redirect_to new_user_registration_url 
    end 
    end 
end 

user.rb

def apply_omniauth(omniauth) 
    self.email = omniauth['user_info']['email'] if email.blank? 
    authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid']) 
end 


def password_required? 
    (authentications.empty? || !password.blank?) && super 
end 

任何幫助將是巨大的,在此先感謝!

回答

13

添加:密碼=> Devise.friendly_token [0,20]從facebook omniauth創建新用戶時。

我相信Devise期待在密碼字段中創建一個用戶。由於在執行facebook oauth(至少不是在您的應用程序端)時沒有密碼,因此您只需創建一個如上所示的虛擬密碼。

更多信息請參閱本: https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

+1

非常感謝,我曾嘗試過,但顯然錯誤地實現這一點。現在一切正常。希望這個鏈接可以幫助別人。 – looloobs

+0

這也幫助我google_oauth2 –

+1

,並可以讓用戶知道密碼是什麼? –

相關問題