2016-08-19 4 views
0

omniauth-facebookomniauth-googleのoAuthを正常に実装しましたが、メールアドレスを提供しないため、omniauth-twitterを実装するのが難しいです。Rails Devise Twitter OAuthはOAuth登録を継続するフォームにリダイレクト

私はインターネット上で利用可能なソリューションを検索しましたが、Deviseの電子メールの検証を削除するという1つの解決方法が指摘されましたが、私のアプリケーションには電子メールの検証が必要です。

oAuthの登録を完了するためにユーザーが新しいメールアドレスを入力する必要がある新しいフォームにユーザーをリダイレクトするだけですが、oAuthデータとリンクして登録を完了する方法がわかりませんユーザー名とパスワードは電子メールだけがそのフォームに表示されている間、空想にすることはできませんので、私はフォームを送信するときにエラーを取得しているので。

CallbackController

def twitter 
     @user = User.from_omniauth(request.env["omniauth.auth"]) 
     if @user.persisted? 
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter" 
      sign_in_and_redirect @user, :event => :authentication 
     else 
      session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra") 
      redirect_to twitter_register_path 
     end 
    end 

twitter_register_pathは、ユーザーが登録を完了するために彼の電子メールアドレスを記入しなければならないのビューが含まれています。

ユーザーモデル

def self.from_twitter_omniauth(auth,email) 
    where(provider: auth["provider"], uid: auth["uid"]).first_or_create do |user| 
     user.provider = auth["provider"] 
     user.uid = auth["uid"] 
     user.email = email 
     user.name = auth["info"]["name"] 
     user.password = Devise.friendly_token[0,20] 
     user.remote_poster_image_url = auth["info"]["image"].gsub('http://','https://') 
    end 
end 

Twitterの登録フォーム(twitter_register_path)

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :email, required: true,label: false %> 
    </div> 

    <div class="signup"> 
    <%= f.button :submit, "Sign up",:class =>"register-button" %> 
    </div> 
<% end %> 

TwitterのAPIは、最近のアプリケーションは、自分の電子メールのために追加の権限を要求することを可能にするにもかかわらず、私はわかりませんよその実装方法の

答えて

1

私はついにそれをこのように解決するために管理しました。

ユーザーがリダイレクトされた後、新規ユーザーの場合は、ユーザーが登録を完了するための電子メールを入力するためのフォームTwitterFormをレンダリングします。

次に、指定したカスタムアクションにフォームが送信されます。私のケースでは、Twitterセッションデータとユーザーが入力した電子メールを使用してサインアッププロセスを完了するUsersControllerの下に置きます。

Deviseの問題のために、CallbacksControllerの中にアクションを一緒に入れることはできませんが、これはエラーがある理由がわかりません。

考案CallbacksController

def twitter 
     auth = request.env["omniauth.auth"] 
     @user = User.where(provider: auth.provider, uid: auth.uid).first_or_create 
     if @user.persisted? 
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter" 
      sign_in_and_redirect @user, :event => :authentication 
     else 
      @form = TwitterForm.new 
      session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra") 
      render :layout => 'none' 
     end 
    end 

がUserController

def twitter_register 
    @form = TwitterForm.new(params[:twitter_form]) 
    if @form.valid? 
     email = params[:twitter_form][:email]  
     @user = User.from_twitter_omniauth(session["devise.twitter_data"],email) 
     if @user.persisted? 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter" 
     sign_in_and_redirect @user, :event => :authentication 
     else 
     redirect_to register_path 
     end 
    else 
     render 'callbacks/twitter',layout: 'none' 
    end 
    end 

TwitterForm

class TwitterForm 
    include ActiveModel::Model 
    attr_accessor :email 
    validates :email, presence: true,:format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ } 
end 

ユーザーモデル

def self.from_twitter_omniauth(auth,email) 
    where(provider: auth["provider"], uid: auth["uid"]).first_or_create do |user| 
     user.provider = auth["provider"] 
     user.uid = auth["uid"] 
     user.email = email 
     user.name = auth["info"]["name"] 
     user.password = Devise.friendly_token[0,20] 
     user.remote_poster_image_url = auth["info"]["image"] 
    end 
end