2016-03-23 5 views
0

Facebookにサインインしようとしたときに「IDを持つユーザーが見つかりませんでした」というエラーが表示されます。preexisting_userとしてセッションにサインインしますか?

def facebook 
    preexisting_user = User.find(params[:id]) # What should go in this line? 
    user = User.from_omniauth(env["omniauth.auth"]) 
    if preexisting_user 
     cookies.permanent.signed[:user_id] = user.id 
     redirect_to root_url 
     flash.now[:info] = 'Welcome Back to Live to Challenge!' 
    else 
     action = session.delete(:challenge_action) 
     user.challenges.create(action: action) 
     user.send_welcome_email 
     user.remember 
     cookies.permanent.signed[:user_id] = user.id 
     redirect_to tutorial_url 
     flash.now[:info] = 'Welcome to Live to Challenge!' 
    end 
    end 

sessions_controller:私は、ユーザーが既にそれは条件付きのコードの最初のセットをトリガー作成されたされ、ユーザーが新しい場合、それは、コードの第2のセットをトリガした場合、それはどこにしたいですあなたは、コードの下に試すことができuser.rb

def self.from_omniauth(auth) 
    # Sets 60 day auth token 
    oauth = Koala::Facebook::OAuth.new("154037ewr2976229929", "ee917abf2ere8f1c98274cdfwqreaebb1346f4") 
    new_access_info = oauth.exchange_access_token_info auth.credentials.token 

    new_access_token = new_access_info["access_token"] 
    new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds 

    where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user| 
     user.provider = auth.provider 
     user.image = auth.info.image 
     user.uid = auth.uid 
     user.name = auth.info.name 
     user.oauth_token = new_access_token # auth.credentials.token <- your old token. Not needed anymore. 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
     user.password = (0...8).map { (65 + rand(26)).chr }.join 
     user.email = SecureRandom.hex + "@mailinator.com" unless user.email.present? 
     user.activated = true 
     user.save! 
    end 
    end 

答えて

0

は、私はそれが動作することを確認しています。

def facebook 
    user = User.from_omniauth(env["omniauth.auth"]) 
    # preexisting_user = User.find(params[:id]) # What should go in this line? 
    # user.persisted? is what you need to add if you want to add here 
    # if user pre existed, it will execute if block otherwise else block. 

    # checks if pre-existing user 
    if user.persisted? 
    cookies.permanent.signed[:user_id] = user.id 
    redirect_to root_url 
    flash.now[:info] = 'Welcome Back to Live to Challenge!' 
    else 
    action = session.delete(:challenge_action) 
    user.challenges.create(action: action) 
    user.send_welcome_email 
    user.remember 
    cookies.permanent.signed[:user_id] = user.id 
    redirect_to tutorial_url 
    flash.now[:info] = 'Welcome to Live to Challenge!' 
    end 
end 
関連する問題