2012-02-20 16 views
1

DevizesとOmniauthを使用して認証を処理するRailsアプリケーションがあります。私はomniauth_callbacks_controllerのプロバイダからコールバックを受けています。ここで認証が既に存在するかどうか、プロバイダが指定した電子メールを持つユーザがすでに存在するかどうかを確認し、必要に応じて新しいユーザを作成します。Devise/Omniauth - 電子メールを含まないプロバイダを扱う方法

各ユーザーに有効なメールが必要です。私の問題はTwitterからのコールバックが付いてくる。 Twitterはそのユーザーのための電子メールを提供しないので、有効なユーザーを作成することはできません。これを解決するために、プロバイダのデータをセッションに保存し、新しい登録ページを送信して、有効なユーザーを作成できるように電子メールアドレスを送信するよう依頼します。このフォームが提出されると、私は問題にぶつかりました。フォームで新しいユーザーが作成されましたが、そのユーザーが既に存在する可能性があります(この場合、そのユーザーに認証を追加する必要があります)。

現時点では、新しいユーザーと同じ電子メールを持つユーザーが既に存在するかどうかを確認しています。もしそうなら、私は新しいユーザーを無視し、既に存在するユーザーに認証を適用します。しかし、これは本当にハッキリと感じます。

どうすればいいですか?

class Users::RegistrationsController < Devise::RegistrationsController 

def build_resource(*args) 
    super 
    if session[:omniauth] 
     #If a user with this email already exists then use them instead 
     existing_user = User.find_by_email(@user.email) 
     if(existing_user) 
      existing_user.email = @user.email 
      @user = existing_user 
     end 
     #If there is a session available, we know it contains data for adding an authentication 
     @user.apply_omniauth_data_as_authentication(session[:omniauth]) 
     #Ensure validations are passed on to next page 
     @user.valid? 
    end 
    end 
+0

これを解決できましたか?私は同じ問題を抱えている。 – Finnnn

+0

@Finnnn答えにOmniauthコールバックコントローラを投稿しました。 – Undistraction

答えて

1

ここに行きます。私がしなければ私が試してみて、私はからの修正を得た場所を覚えて、リンクを投稿します:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 

    # This controller handles callbacks from Omniauth providers. If a user already exists with this provider we sign them in. 
    # Otherwise we either add an authentication to the current user, or create a new user if one doesn't exist. 

    def facebook 
    authorize 
    end 

    def twitter 
    authorize 
    end 

    private 

    def authorize 
     omniauth_data = request.env["omniauth.auth"] 
     #Check to see if we have an authentication for this provider already stored for any User 
     authentication = Authentication.find_by_provider_and_uid(omniauth_data['provider'], omniauth_data['uid']) 
     #If an authentication already exists, sign the owning User in 
     if authentication 
     flash[:notice] = "Signed in successfully with " + omniauth_data['provider'].titleize 
     sign_in_and_redirect(:user, authentication.user) 
     #Otherwise if there is a current User, add a new authentication for them 
     elsif current_user 
     current_user.authentications.create(:provider => omniauth_data['provider'], :uid => omniauth_data['uid'], :token => omniauth_data['token']) 
     flash[:notice] = "Authentication successful" 
     redirect_to user_profile_url 
     #Otherwise we check if a user exists with the email address provided by the provider (if provided at all)) 
     else 
     email = omniauth_data['info']['email'] 
     user = User.find_by_email(email) 
     #If no user exists, create a new one 
     if(!email || !user) 
      user = User.new 
     end 
     user.apply_omniauth_data_as_authentication(omniauth_data) 
     #If they save successfully (meaning we have enough data from the authorisation) sign them in 
     if user.email? 
      #We got all we needed from the provider so we don't need them to confirm' 
      user.skip_confirmation! 
      user.save! 
      flash[:notice] = "Signed in successfully with " + omniauth_data['provider'] 
      sign_in_and_redirect(:user, user) 
     #Otherwise we need to have the visitor manually submit missing information 
     else 
      #save the omniauth data in a session so we can add the authentication once registration is complete 
      flash[:alert] = "Please complete registration" 
      session[:omniauth] = omniauth_data.except('extra') 
      redirect_to new_user_registration_url 
     end 
     end 
    end 

end 
0

私は、このリンクhttp://www.orhancanceylan.com/rails-twitter-and-facebook-authentications-with-omniauth-and-devise/

で使用されるソリューションが好きそれは通常に登録するプロバイダの資格情報を使用しています(ユーザーや別のテーブルのproviderとuid属性を記入するだけでなく)プロセスを作成します。したがって、新しいユーザーがプロバイダを使用して登録しようとすると、メールやパスワードを入力する必要があります。通常の方法でログインしたいが、入力するパスワードがないなど、それを処理する必要があります)。

このソリューションは、最初にソーシャルメディアに登録する利便性をいくらか無効にしますが、最小限の労力で長期的な有益な効果をもたらします。

申し込みを失う恐れやウェブサイトの磨耗が少ないことなど、この種の要件を絶対に避けたい場合は、別の解決策を見つけて、それらのダウンストリームケースを処理するすべての作業を行います。

関連する問題