ruby-on-rails - Omniauth Facebook 重定向到注册屏幕而不是创建用户

标签 ruby-on-rails facebook controller omniauth omniauth-facebook

按照 gem 的教程完全,我似乎仍然遇到了一些问题。我首先单击链接以通过 Facebook 注册并重定向到 Facebook。我单击确定并返回到我的注册页面。没有创建新用户。如果我尝试再次单击注册链接,它只会在地址栏 http://localhost:3000/register#_=_ 中刷新页面。我做错了什么?

用户模型

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]

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.first_name = auth.info.first_name
    user.last_name = auth.info.last_name
  end
end

回调 Controller

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @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
end

控制台代码

Started GET "/auth/facebook/callback?code=AQCh2rckn0dMOKNggTQJEbx_LTMGjNrDdumTu_Ay9Cwr_87A2pro_gOgFtSa866DouLuj4kMLVMMPfR3amp_X6rNSlAwOQ51RXK053vxhtEyJNY8StcqiOVZ6YqpGmfg8Zsz8RIXQr2xRclPmW9j9o8fqg83CoFiH1j-e_YOOuyCaX6xkoVN1eC3kjWSezg_WFm3p6aXu_XYxw1-Jf2j3Ijqt4kR4ySLHfmauj1z9UUcy5HANMIjaRx84NiZHNdJq6fzuAT37y9cXvP5HawonBBA0bG7ejAk5AATBYJj-kTXdCR9673cQ55O4GbAw_LCzzy7_YgHkFR9S038tP68HnXm&state=d013067bc67bd4bff43b6ac77bea69f5b113858cb234452a" for ::1 at 2015-07-12 19:28:55 -0400
I, [2015-07-12T19:28:55.026584 #7266]  INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#facebook as HTML
  Parameters: {"code"=>"AQCh2rckn0dMOKNggTQJEbx_LTMGjNrDdumTu_Ay9Cwr_87A2pro_gOgFtSa866DouLuj4kMLVMMPfR3amp_X6rNSlAwOQ51RXK053vxhtEyJNY8StcqiOVZ6YqpGmfg8Zsz8RIXQr2xRclPmW9j9o8fqg83CoFiH1j-e_YOOuyCaX6xkoVN1eC3kjWSezg_WFm3p6aXu_XYxw1-Jf2j3Ijqt4kR4ySLHfmauj1z9UUcy5HANMIjaRx84NiZHNdJq6fzuAT37y9cXvP5HawonBBA0bG7ejAk5AATBYJj-kTXdCR9673cQ55O4GbAw_LCzzy7_YgHkFR9S038tP68HnXm", "state"=>"d013067bc67bd4bff43b6ac77bea69f5b113858cb234452a"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."provider" = ? AND "users"."uid" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["provider", "facebook"], ["uid", "836028396465704"]]
   (0.1ms)  begin transaction
   (0.0ms)  rollback transaction
Redirected to http://localhost:3000/register
Completed 302 Found in 59ms (ActiveRecord: 0.2ms)

最佳答案

将这一行添加到 config/initializers/devise.rb

config.omniauth :facebook, "app id", "app secret", scope: 'email', info_fields: 'email'

新版本的 Facebook API 有一个变化, Introducing Graph API v2.4

Fewer default fields for faster performance: To help improve performance on mobile network connections, we've reduced the number of fields that the API returns by default. You should now use the ?fields=field1,field2 syntax to declare all the fields you want the API to return.

关于ruby-on-rails - Omniauth Facebook 重定向到注册屏幕而不是创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373640/

相关文章:

ruby-on-rails - Rails 更改区域设置返回当前页面

iOS 9 Facebook SDK 登录/分享

zend-framework - 在 Bootstrap 中获取 Controller 名称

testing - grails:如何使用多个操作和多个重定向来测试 Controller ?

html - GMail 中 <td> 的宽度无效

ruby-on-rails - Rspec 在 View 测试中与装饰器上的助手有关的问题

javascript - 如何在js中使用facebook api请求用户权限?

php - 使用 Symfony 检查 Controller 内是否处于 "dev"模式

ruby-on-rails - 我可以将 Rails 属性命名为 "attribute?"

ios - 使用 map 创建开放图谱故事 - iOS