在before_filter上使用Rails和Omniauth重定向循环

Jul*_*ina 4 facebook ruby-on-rails omniauth ruby-on-rails-3

我目前正在开发一个Facebook应用程序,要求用户在能够访问该应用程序之前登录.

我有一before_filterapplication_controller来检查用户登录,如果不是他们重定向到路径正确的符号.

这是迄今为止的代码:

Application Controller

before_filter :check_sign_in

def check_sign_in
  unless user_signed_in?
  redirect_to signin_path
end

private 
def current_user
  begin
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  rescue Mongoid::Errors::DocumentNotFound
    nil
  end
end

def user_signed_in?
  return true if current_user
end
Run Code Online (Sandbox Code Playgroud)

我有我routes.rb的以下内容signin_path

match '/signin' => 'sessions#new', :as => :signin
Run Code Online (Sandbox Code Playgroud)

我的 sessions#new行动如下

def new
  redirect_to '/auth/facebook'
end
Run Code Online (Sandbox Code Playgroud)

手头的问题虽然是这个我得到一个重定向循环错误,其读取如下

此网页有一个重定向循环http:// localhost:3000/signin
的网页导致了太多的重定向.清除此站点的cookie或允许第三方cookie可以解决问题.如果没有,它可能是服务器配置问题,而不是您的计算机的问题.

这似乎很可能是因为facebook的登录过程中还有一个回调,我怀疑是什么导致循环.有没有更好的方法来确保用户登录或其他方法来实现我的想法?

我目前正在使用Rails 3.1.3,
Ruby 1.9.3,
omn​​iauth-facebook 1.2.0,
omn​​iauth 1.0.2

Bra*_*est 5

在你的SessionsController中你需要添加skip_before_filter :check_sign_in, :only => [:new, :create].

在没有条件的情况下在ApplicationController中添加before_filter意味着每个控制器中从其继承的每个操作也将被重定向,因此实际的会话操作将永远不会被调用.

您可能还需要跳过OmniAuthCallbackController中的before_filter,具体取决于它是否继承自ApplicationController.