Omniauth-facebook取消按钮

时间:2012-05-10 01:30:58

标签: facebook devise omniauth

我已经开始将facebook身份验证集成到我的Rails 3.1站点中,但是当我单击fb auth对话框上的取消按钮时遇到了问题。当我点击取消时,我会被重定向回我的网站/ auth / facebook / callback,然后重定向到/ login页面(我正在使用Devise)。

我想要做的是将取消的身份验证重定向到允许用户以标准方式(电子邮件,用户名,密码等)创建帐户的页面。如何覆盖重定向到/ login页面?

是的,我正在使用omniauth-facebook gem。

谢谢!

2 个答案:

答案 0 :(得分:3)

在omniauth回调控制器中添加失败方法并定义自定义行为。

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
        @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

    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 failure
    redirect_to root_path // here
  end

end

答案 1 :(得分:0)

我认为您可能能够覆盖omniauth配置中的默认on_failure行为,我没有使用设计,但使用的是omniauth-facebook gem并且已经成功使用了变体:

OmniAuth.config.on_failure = Proc.new { |env|
  OmniAuth::FailureEndpoint.new(env).redirect_to_failure
} 

或者更像自定义的东西:

OmniAuth.config.on_failure do |env|
  new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{error_type}"

  [302, {'Location' => new_path, 'Content-Type'=> 'text/html'}, []]
end