通过facebook登录重定向到注册路径

时间:2014-09-28 08:51:46

标签: ruby-on-rails devise omniauth omniauth-facebook

我有问题通过Facebook登录 我使用mongoid 4.0.0,rails 4.1.5,设计3.3.0和omniauth-facebook 2.0.0

我使用这个例子https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

如果我尝试通过Facebook登录,则会将我重定向到注册页面

http://localhost:3000/users/registration#_=_

日志文件

Started GET "/users/auth/facebook" for 127.0.0.1 at 2014-09-28 12:48:22 +0400
I, [2014-09-28T12:48:22.189885 #1102]  INFO -- omniauth: (facebook) Request phase initiated.


Started GET "/users/auth/facebook/callback?code=AQAM69gxg_GR3sGFmIM16U2AfNUM-UdImifF2qNbJz0xiiWVRMCjc_gFSncvkeeSDbiv0pMwLkS7zG5HQoEHmlG7e1tnZYcnqYMEh_YEsstNucLTWQRYcjIpsHCzymDrW4u7xYm-YtRvghLABMmlExkAsJ4MZ4AJ2aI6bMWHF_mG9d1hlZ5x2MnsbJGDXNp62qQh2U7j6njD3spLPco22s4LVRPsehbI4o7JlY58KwGaAmzXM_FKbA_nERI_1JCt56x8PFQvE4c4d75ABjgkYyoIj4DTXdjRp-nJ1JvlK_FZWT6kqTONcw_KaT2B0iXujogB-QP2SikXwgN_N0e_1GhW&state=4e58d1a68766eab76419e283e89a9f5ad6d8fa6f3c0ab870" for 127.0.0.1 at 2014-09-28 12:48:22 +0400
I, [2014-09-28T12:48:22.506731 #1102]  INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#facebook as HTML
  Parameters: {"code"=>"AQAM69gxg_GR3sGFmIM16U2AfNUM-UdImifF2qNbJz0xiiWVRMCjc_gFSncvkeeSDbiv0pMwLkS7zG5HQoEHmlG7e1tnZYcnqYMEh_YEsstNucLTWQRYcjIpsHCzymDrW4u7xYm-YtRvghLABMmlExkAsJ4MZ4AJ2aI6bMWHF_mG9d1hlZ5x2MnsbJGDXNp62qQh2U7j6njD3spLPco22s4LVRPsehbI4o7JlY58KwGaAmzXM_FKbA_nERI_1JCt56x8PFQvE4c4d75ABjgkYyoIj4DTXdjRp-nJ1JvlK_FZWT6kqTONcw_KaT2B0iXujogB-QP2SikXwgN_N0e_1GhW", "state"=>"4e58d1a68766eab76419e283e89a9f5ad6d8fa6f3c0ab870"}
  MOPED: 54.196.12.51:10063 COMMAND      database=admin command={:ismaster=>1} runtime: 146.1740ms
  MOPED: 54.196.12.51:10063 QUERY        database=arch_dev collection=users selector={"$query"=>{"provider"=>"facebook", "uid"=>"736207756417197"}, "$orderby"=>{"created_at"=>-1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 140.1610ms
Redirected to http://localhost:3000/users/registration
Completed 302 Found in 1888ms

omniauth_callbacks_controller.rb

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

user.rb

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.name = auth.info.name
      user.username = auth.info.last_name
      user.avatar_url = auth.info.image
    end
  end

2 个答案:

答案 0 :(得分:1)

问题出在Facebook权限

没有朋友的用户无法通过Facebook注册

答案 1 :(得分:0)

用户没有被持久化。那你为什么不尝试更简单的方法呢?在控制器中创建用户。您可以检查以下代码以创建用户,然后从设计创建会话并重定向到主页。

@user = User.new :name => auth_hash["info"]["name"], :username => auth_hash["info"]["last_name"], :email => auth_hash["info"]["email"] , :fb_profile_url => auth_hash["info"]["image"]

sign_in_and_redirect @user