2011-07-10 4 views
8

私はDevise with Omniauthを使って、ユーザーがFacebookで自分のアプリにサインインするようにしています。 Railscastチュートリアルを使って起動しました。Devise Omniauth "新しいユーザーのためにencrypted_pa​​sswordがNULLになることはありません"

ユーザーがすでにFacebookのメンバーによる認証を受けている場合は、Facebookでの認証がうまく動作します。この問題は、facebookで新しいユーザーを認証するときに発生します。ユーザーモデル用の新しいユーザーを作成すると、「users.encrypted_pa​​sswordがNULLでない可能性があります」というエラーが表示されます。 Facebookの情報からパスワードをUserモデルに渡す方法を理解できません。

は、これは私が持っているものです。

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

Add:パスワード=> Devise.friendly_token [0,20] facebook omniauthから新しいユーザーを作成するとき。

私はDeviseがユーザーを作成するためにパスワードフィールドに何かを期待していると思います。 Facebookのoauth(少なくともあなたのアプリ側ではない)を実行するときにはパスワードがないので、上記のようにダミーのパスワードを作成する必要があります。

は、より多くの情報のためにこれを参照してください。 https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

+1

おかげで多くのことを、私は前にこれを実装しようとしたが、明らかに間違っていました。すべては現在正常に動作しています。うまくいけば、そのリンクは他の誰かを助けるかもしれない。 – looloobs

+0

これはまた、google_oauth2 –

+1

と私を助け、ユーザーはパスワードが何であるか知ることができますか? –

関連する問題