2017-07-18 4 views
0

私は、複数のプロバイダがOmniauthを取得するために数日間苦労しています。私は最終的に(ほとんど)働いている - FacebookとTwitterは同期のために期待通りに動作するが、私はSteamに問題がある。あなたがtwitter機能を見ることができるように未定義のメソッドを返すが、とにかく保存するレールコントローラ

class AuthenticationsController < ApplicationController 
    skip_before_action :verify_authenticity_token, :only => :steam 

    def index 
    @authentications = current_user.authentications.all 
    end 
    # <%= link_to 'Authentications', authentications_path %> 
    def home 
    end 

    def twitter 
    omni = request.env["omniauth.auth"] 
    authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

    if authentication 
     flash[:notice] = "Logged in Successfully" 
     sign_in_and_redirect User.find(authentication.user_id) 
    elsif current_user 
     token = omni['credentials'].token 
     token_secret = omni['credentials'].secret 

     current_user.authentications.create!(:provider => omni['provider'], 
              :uid => omni['uid'], 
              :token => token, 
              :token_secret => token_secret) 
     flash[:notice] = "Authentication successful." 
     sign_in_and_redirect current_user 
    else 
     user = User.new 
     user.apply_omniauth(omni) 
     raise user.inspect 
     if user.save 
     flash[:notice] = "Logged in." 
     sign_in_and_redirect User.find(user.id)    
     else 
     session[:omniauth] = omni.except('extra') 
     redirect_to new_user_registration_path 
     end 
    end 
    end 

    def destroy 
    @authentication = Authentication.find(params[:id]) 
    @authentication.destroy 
    redirect_to authentications_url, :notice => "Successfully destroyed authentication." 
    end 


    def facebook 
    omni = request.env["omniauth.auth"] 
    authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

    if authentication 
     flash[:notice] = "Logged in Successfully" 
     sign_in_and_redirect User.find(authentication.user_id) 
    elsif current_user 
     token = omni['credentials'].token 
     token_secret = "" 

     current_user.authentications.create!(:provider => omni['provider'], 
              :uid => omni['uid'], 
              :token => token, 
              :token_secret => token_secret) 

     flash[:notice] = "Authentication successful." 
     sign_in_and_redirect current_user 
    else 
     user = User.new 
     user.email = omni['extra']['raw_info'].email 

     user.apply_omniauth(omni) 

     if user.save 
     flash[:notice] = "Logged in." 
     sign_in_and_redirect User.find(user.id)    
     else 
     session[:omniauth] = omni.except('extra') 
     redirect_to new_user_registration_path 
     end 
    end 
    end 

    def steam 
     omni = request.env["omniauth.auth"] 
     authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

     if authentication 
      flash[:notice] = "Logged in Successfully" 
      sign_in_and_redirect User.find(authentication.user_id) 
     elsif current_user 
     token = omni['extra']['raw_info'].steamid 
     # render :text => request.env["omniauth.auth"].info.to_hash.inspect 

     puts token 
     token_secret = "" 

      current_user.authentications.create!(:provider => omni['provider'], 
               :uid => omni['uid'], 
               :token => token, 
               :token_secret => token_secret) 
      flash[:notice] = "Authentication successful." 
      sign_in_and_redirect current_user 
     else 
      user = User.new 
      user.apply_omniauth(omni) 
     end 

     if user.save 
      flash[:notice] = "Logged in." 
      sign_in_and_redirect User.find(user.id)    
     else 
      session[:omniauth] = omni.except('extra') 
      redirect_to new_user_registration_path 
     end 
    end 




end 

steamはほとんど重複している - 彼らは、あるUser model内の関数を参照する

def apply_omniauth(omni) 
    authentications.build(:provider => omni['provider'], 
          :uid => omni['uid'], 
          :token => omni['credentials'].token, 
          :token_secret => omni['credentials'].secret) 
    end 

現在、蒸気が上undefined method 'save' for nil で応答します

if user.save 
    flash[:notice] = "Logged in." 
    sign_in_and_redirect User.find(user.id)    
else 

私が混乱しているのは、実際に保存していることです。

=> #<Authentication id: 12, provider: "steam", uid: "redacted", token: "76561198038103683", token_secret: "", created_at: "2017-07-18 14:57:32", updated_at: "2017-07-18 14:57:32", user_id: 2> 
2.4.1 :022 > u.authentications.last.destroy 
    Authentication Load (0.8ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."user_id" = $1 ORDER BY "authentications"."id" DESC LIMIT $2 [["user_id", 2], ["LIMIT", 1]] 
    (0.3ms) BEGIN 
    SQL (0.6ms) DELETE FROM "authentications" WHERE "authentications"."id" = $1 [["id", 13]] 
    (4.2ms) COMMIT 
=> #<Authentication id: 13, provider: "steam", uid: "redacted", token: "redacted", token_secret: "", created_at: "2017-07-18 15:13:00", updated_at: "2017-07-18 15:13:00", user_id: 2> 
2.4.1 :023 > u.authentications.last 
    Authentication Load (0.6ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."user_id" = $1 ORDER BY "authentications"."id" DESC LIMIT $2 [["user_id", 2], ["LIMIT", 1]] 
=> #<Authentication id: 14, provider: "steam", uid: "redacted", token: "redacted", token_secret: "", created_at: "2017-07-18 15:13:19", updated_at: "2017-07-18 15:13:19", user_id: 2> 

レコードを削除してから再認証して同じエラーが発生しますが、関係は存在します。

私はuserをどのように印刷してそれが起こっているのか、またはこの中でどのようにバグテストするのかを知りません。

ご意見は歓迎します。

答えて

0

したがってuser変数はelseブランチでのみ設定しています。他のブランチが評価された場合、user変数はゼロです。ただ、第二のelseブランチに(if user.save)であれば、最後に移動した場合:

def steam 
    omni = request.env["omniauth.auth"] 
    authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

    if authentication 
     flash[:notice] = "Logged in Successfully" 
     sign_in_and_redirect User.find(authentication.user_id) 
    elsif current_user 
    token = omni['extra']['raw_info'].steamid 
    # render :text => request.env["omniauth.auth"].info.to_hash.inspect 

    puts token 
    token_secret = "" 

     current_user.authentications.create!(:provider => omni['provider'], 
              :uid => omni['uid'], 
              :token => token, 
              :token_secret => token_secret) 
     flash[:notice] = "Authentication successful." 
     sign_in_and_redirect current_user 
    else 
     user = User.new 
     user.apply_omniauth(omni) 
     if user.save 
      flash[:notice] = "Logged in." 
      sign_in_and_redirect User.find(user.id)    
     else 
      session[:omniauth] = omni.except('extra') 
      redirect_to new_user_registration_path 
     end 
    end 
end 
+0

私はそのようにそれについて考えていなかったが、私はそれを得ます。私が苦労しているのは、技術的には、「else」を打つべきではありません。それが私を同期させると、 'elsif current_user'に当たってから続けます。私はレールに新鮮ですが、なぜそれがelsifをやっているのか、そしてelseをやっているのか分かりません。 – DNorthrup

+0

あなたは 'else'をやっていません:)' sign_in_and_redirect'はコードの残りの部分が実行されるのを止めないので、次の 'if'を実行しています。このメソッドのあとに 'return'を追加すると、実行されないでしょう。 – Ptr

+0

詳細をお寄せいただきありがとうございます。私はそれを手に入れますが、完全にはありません。私はuser.saveの場合は上記の 'else'以上のリターンを試みましたが、user.saveでエラーが発生しています - あなたが意味していることの例を表示できますか? – DNorthrup

関連する問題