2016-07-08 3 views
0

私はomniauth-facebookでDeviseを正常に実装しましたが、Twitterで手を入れています。私はかなりFacebookのDevise settingsをTwitter用のメソッドにコピーしました。しかし、自分のTwitterアカウントを使用するようにアプリを正常に承認した後、ユーザー登録ページ(http://localhost:3000/users/sign_up)にリダイレクトされます。どうしてこれなの?Devise Twitter Omniauthがsign_upにリダイレクト

Console output

Started GET "https://stackoverflow.com/users/auth/twitter/callback?oauth_token=zeIyTgAAAAAAwAQEAAABVcusPxc&oauth_verifier=q24BAAziukc8bF6nnaxuRoouuGaPuoF3" for ::1 at 2016-07-08 14:01:51 -0400 
I, [2016-07-08T14:01:51.984997 #44805] INFO -- omniauth: (twitter) Callback phase initiated. 
Processing by Users::OmniauthCallbacksController#twitter as HTML 
    Parameters: {"oauth_token"=>"zeIyTgAAAAAAwAQEAAABVcusPxc", "oauth_verifier"=>"q24BAAziukc8bF6nnaxuRoouuGaPuoF3"} 
    User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "twitter"], ["uid", "248829852"]] 
    (0.2ms) BEGIN 
    (0.1ms) ROLLBACK 
Redirected to http://localhost:3000/users/sign_up 
Completed 302 Found in 165ms (ActiveRecord: 1.0ms) 

controllers/users/omniauth_callbacks_controller.rb

クラスユーザー:: OmniauthCallbacksController <考案:: OmniauthCallbacksController DEF投稿@user = User.from_omniauth(request.env [ "omniauth.auth"])

if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 

    def twitter 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format? 
    else 
     session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra") 
     redirect_to new_user_registration_url 
    end 
    end 

    def failure 
    redirect_to root_path 
    end 
end 

models/user.rb

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, 
     :omniauthable, :omniauth_providers => [:facebook, :twitter] 

    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
    user.email = auth.info.email 
    user.password = Devise.friendly_token[0,20] 
    user.name = auth.info.name # assuming the user model has a name 
    user.image = auth.info.image # assuming the user model has an image 
    end 
end 

end 

config/routes.rb

Rails.application.routes.draw do 

    devise_for :users, 
    :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } 


    root 'welcome#index' 
end 
+0

を、あなたのモデル内のメールの存在を確保するために、任意の検証をやっていますか? – oreoluwa

+0

実際、いいえ。私はまだバリデーションを行っていません...私のDBにはフィールドがありますが、この時点でユーザーモデルには検証はありません。 – Godzilla74

+1

あなたは 'Twitter'ユーザに一時的な電子メールを割り当て、何が起こるかを知るべきだと思います。 – oreoluwa

答えて

2

コミットからトランザクションを防止正確なエラーを表示するには、このようなあなたのTwitterのコールバックにデバッグ出力を追加します。

def twitter 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format? 
    else 
     session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra") 

     puts @user.errors 

     redirect_to new_user_registration_url 
    end 
    end 

それは復帰しない場合がありますので、Twitterのは、登録時に電子メールを必要としないことが知られていますそのようなアカウントに対してoauthを実行しているときの値。

問題点として、Deviseはデフォルトでいくつかのフィールドの自動検証を提供しています。 Devise sourceから:

def self.included(base) 
    base.extend ClassMethods 
    assert_validations_api!(base) 

    base.class_eval do 
     validates_presence_of :email, if: :email_required? 

あなたUserモデルでこれを入れて、デフォルトの電子メールの有無の検証をオフにする:

def email_required? 
    false 
end 

あなたのユースケースに応じて、あなたもしないように、デフォルトの認証キーを変更することもできます電子メールを使用します。

設定/初期化子/ devise.rbに:

0123考案提供ジェネレータを使用する場合

UPDATE

userテーブルがemailフィールドにnull: false制約で終わることができます。 、それを削除して、移行を作成するには:

change_column :users, :email, :string, :null => true 

とデータベーススキーマ更新:

bundle exec rake db:migrate 
+0

'email_requiredを追加しましたか?'メソッド...しかし、私は別の怒っているエラーを取得しています:' ActiveRecord :: StatementInvalid(PG :: NotNullViolation:ERROR:列のnull値の電子メール "not-null constraint'に違反しています。 – Godzilla74

+0

マイグレーション中のデータベースの 'users.email'から' null:false'制約を削除してください –

+0

私の答えの更新を見てください。 –

関連する問題