如何将Facebook帐户与Devise合并到现有帐户中

时间:2013-01-25 08:44:36

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

我按照here步骤操作,并在合并帐户时遇到了一些问题

根据说明,我将以下方法添加到我的ApplicationController

def after_sign_in_path_for(resource)
  # Merge data from Facebook with her current account
  if session["facebook_data"] && current_user.facebook_uid.nil?
    current_user.facebook_uid = session["facebook_data"]["uid"]
    current_user.save(:validate => false)
  end

  # Countermeasure against session fixation
  session.keys.grep(/^facebook\./).each { |k| session.delete(k) }

  super
end

但是,我在ApplicationController中得到一个空会话[“facebook_data”],即使我在调用此方法的回调app / controllers / users / omniauth_callbacks_controller.rb中看到正确的会话值:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
    if user && user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect user, :event => :authentication
    else
      session["facebook_data"] = env["omniauth.auth"]
      redirect_to new_user_session_url # this redirect invokes the after_sign_in_path_for method in ApplicationController
    end
  end

end

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

出于多种原因,值得检查的两个原因是:

1) session["facebook_data"]中的ApplicationControlleromniauth_callbacks_controller.rb中设置之前被调用。

解决方案:检查两个控制器的呼叫是否正确同步

  • 应该先omniauth_callbacks_controller.rb然后ApplicationController

2)session["facebook_data"]中设置后,omniauth_callbacks_controller.rb的值会重置为 null

解决方案:在session["facebook_data"]中设置的omniauth_callbacks_controller.rb值跟踪session["facebook_data"]中对ApplicationController的呼叫后跟踪来电。