私は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
を、あなたのモデル内のメールの存在を確保するために、任意の検証をやっていますか? – oreoluwa
実際、いいえ。私はまだバリデーションを行っていません...私のDBにはフィールドがありますが、この時点でユーザーモデルには検証はありません。 – Godzilla74
あなたは 'Twitter'ユーザに一時的な電子メールを割り当て、何が起こるかを知るべきだと思います。 – oreoluwa