繁体   English   中英

设计Twitter Omniauth重定向到sign_up

[英]Devise Twitter Omniauth redirects to sign_up

我已经使用omniauth-facebook成功实现了Devise,现在正在Twitter上尝试一下。 我已经将Facebook的Devise设置复制到Twitter的方法中。 但是,在成功批准我的应用程序使用我的Twitter帐户后,我被重定向回我的用户注册页面( http:// localhost:3000 / users / sign_up )。 为什么是这样?

控制台输出

Started GET "/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)

控制器/用户/omniauth_callbacks_controller.rb

类Users :: OmniauthCallbacksController <Devise :: OmniauthCallbacksController def facebook @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

型号/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

像这样将调试输出添加到您的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来源

  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

根据您的用例,您可能还需要更改默认身份验证密钥以不使用电子邮件:

config / initializers / devise.rb中

config.authentication_keys = [ :username ]

更新

当使用由Devise提供的生成器时, user表的结尾可能为null: false email字段上的null: false约束。 要删除它,请使用以下方法创建迁移:

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

并更新数据库架构:

bundle exec rake db:migrate

我在使用Github身份验证时遇到了同样的错误,最后通过将范围添加到devise.rbconfig.omniauth中,它起作用了! 也许它需要范围用户来检索电子邮件和其他信息

config.omniauth :github, ENV['GITHUB_APP_ID'], ENV['GITHUB_APP_SECRET'], scope: 'repo,user'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM