Omniauth Facebook 重定向到注册屏幕而不是创建用户 [英] Omniauth Facebook redirects to registration screen instead of creating user

查看:12
本文介绍了Omniauth Facebook 重定向到注册屏幕而不是创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following the gem's tutorial exactly I still seem to be running into some issues. I first click the link to sign up via Facebook and get redirected to Facebook. I click okay and get brought back to my registration page. No new user is created. If I try to click the register link again it just refreshes the page with this in the address bar http://localhost:3000/register#_=_. What am I doing wrong?

用户模型

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

回调控制器

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

控制台代码

<代码>发起者GET /AUTH/实/回调代码= AQCh2rckn0dMOKNggTQJEbx_LTMGjNrDdumTu_Ay9Cwr_87A2pro_gOgFtSa866DouLuj4kMLVMMPfR3amp_X6rNSlAwOQ51RXK053vxhtEyJNY8StcqiOVZ6YqpGmfg8Zsz8RIXQr2xRclPmW9j9o8fqg83CoFiH1j-e_YOOuyCaX6xkoVN1eC3kjWSezg_WFm3p6aXu_XYxw1-Jf2j3Ijqt4kR4ySLHfmauj1z9UUcy5HANMIjaRx84NiZHNdJq6fzuAT37y9cXvP5HawonBBA0bG7ejAk5AATBYJj-kTXdCR9673cQ55O4GbAw_LCzzy7_YgHkFR9S038tP68HnXm&安培;状态= d013067bc67bd4bff43b6ac77bea69f5b113858cb234452a" 为:: 1在2015年7月12日19时28分55秒-0400I, [2015-07-12T19:28:55.026584 #7266] INFO -- omniauth: (facebook) 回调阶段启动.由用户处理::OmniauthCallbacksController#facebook 作为 HTML参数:{ 代码"=> 中AQCh2rckn0dMOKNggTQJEbx_LTMGjNrDdumTu_Ay9Cwr_87A2pro_gOgFtSa866DouLuj4kMLVMMPfR3amp_X6rNSlAwOQ51RXK053vxhtEyJNY8StcqiOVZ6YqpGmfg8Zsz8RIXQr2xRclPmW9j9o8fqg83CoFiH1j-e_YOOuyCaX6xkoVN1eC3kjWSezg_WFm3p6aXu_XYxw1-Jf2j3Ijqt4kR4ySLHfmauj1z9UUcy5HANMIjaRx84NiZHNdJq6fzuAT37y9cXvP5HawonBBA0bG7ejAk5AATBYJj-kTXdCR9673cQ55O4GbAw_LCzzy7_YgHkFR9S038tP68HnXm", 状态"=> 中d013067bc67bd4bff43b6ac77bea69f5b113858cb234452a"}用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."provider" = ?和用户".uid"=?ORDER BY "users"."id" ASC LIMIT 1 [["provider", "facebook"], ["uid", "836028396465704"]](0.1ms) 开始事务(0.0ms) 回滚事务重定向到 http://localhost:3000/register在 59ms 内完成 302 Found (ActiveRecord: 0.2ms)

推荐答案

将此行添加到 config/initializers/devise.rb

Add this line to config/initializers/devise.rb

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

新版 Facebook API 发生了变化,介绍Graph API v2.4

There's a change in the new version of Facebook API, Introducing Graph API v2.4

减少默认字段以提高性能:为了帮助提高移动网络连接的性能,我们减少了 API 默认返回的字段数量.您现在应该使用 ?fields=field1,field2 语法来声明您希望 API 返回的所有字段.

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.

这篇关于Omniauth Facebook 重定向到注册屏幕而不是创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆