使用 Rails & 重定向循环before_filter 上的 Omniauth
我目前正在开发一个 Facebook 应用程序,该应用程序需要用户登录才能访问该应用程序。
我在 application_controller
上有一个 before_filter
来检查用户是否已登录,如果未登录,则将其重定向到正确的登录路径。
这是到目前为止的代码:
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
我有我的 routes.rb
我有以下 signin_path
match '/signin' => 'sessions#new', :as => :signin
我的 sessions#new
操作如下
def new
redirect_to '/auth/facebook'
end
问题不过,目前我收到了一个重定向循环错误,其内容如下
该网页存在重定向循环
http://localhost:3000/signin 网页导致了过多的重定向。清除该网站的 cookie 或允许第三方 cookie 可能会解决该问题。如果不是,则可能是服务器配置问题,而不是您的计算机问题。
这似乎很有可能,因为 Facebook 的登录过程也有一个回调,我怀疑这是导致循环的原因。是否有更好的方法来确保用户登录或使用其他方法来实现我的想法?
我目前正在使用 Rails 3.1.3,
红宝石 1.9.3,
omniauth-facebook 1.2.0,
全方位认证 1.0.2
I am currently developing a facebook app which will require the user to be signed in prior to being able to access the app.
I have a before_filter
on the application_controller
to check if the user is logged in and if not redirect them to the proper sign in path.
This is the code thus far:
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
I have my routes.rb
I have the following for signin_path
match '/signin' => 'sessions#new', :as => :signin
My sessions#new
action looks as follows
def new
redirect_to '/auth/facebook'
end
The problem at hand though is with this I am getting a redirect loop error on which is reading as follows
This webpage has a redirect loop
The webpage at http://localhost:3000/signin has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
This seems to be more than likely because the sign in process to facebook also has a callback which I suspect is whats causing the loop. Is there a better approach to ensure a user is logged or another method to go about achieving what I had in mind?
I am currently using
Rails 3.1.3,
Ruby 1.9.3,
omniauth-facebook 1.2.0,
omniauth 1.0.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 SessionsController 中,您需要添加
skip_before_filter :check_sign_in, :only =>; [:新建,:创建]
。在不带条件的情况下在 ApplicationController 中添加 before_filter 意味着继承它的每个控制器中的每个操作也将被重定向,因此永远不会调用实际的会话操作。
您可能还需要跳过 OmniAuthCallbackController 中的 before_filter,具体取决于它是否也继承自 ApplicationController。
In your SessionsController you need to add
skip_before_filter :check_sign_in, :only => [:new, :create]
.Adding the before_filter in the ApplicationController without conditions means every single action in every controller that inherits from it will also be redirected, so the actual sessions action will never be called.
You may need to also skip the before_filter in your OmniAuthCallbackController depending on whether it is inheriting from ApplicationController as well.