我正在使用Omnisuth設計讓用戶使用Facebook登錄我的應用程序。我使用Railscast教程來啓動並運行它。爲新用戶設計Omniauth「encrypted_password不能爲NULL」
如果用戶已經是我的網站的成員通過臉書認證工作正常。使用Facebook驗證新用戶時出現問題。當它爲我的用戶模型創建一個新用戶時,我得到「users.encrypted_password可能不是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
任何幫助將是巨大的,在此先感謝!
非常感謝,我曾嘗試過,但顯然錯誤地實現這一點。現在一切正常。希望這個鏈接可以幫助別人。 – looloobs
這也幫助我google_oauth2 –
,並可以讓用戶知道密碼是什麼? –