September 3, 2013

Infinite redirect loop during authentication

Did you ever see following error? Especially with monban authentication gem?

Failure/Error: visit root_path
Capybara::InfiniteRedirectError:
  redirected more than 5 times, check for infinite redirects.

Well it can easily happen when you use something like this in your application controller:

class ApplicationController < ActionController::Base
  include Monban::ControllerHelpers
  before_action :require_login
  ...
end

I.e. you want to authenticate users for every action in your application.

This is the solution:

class SessionsController < ApplicationController
  skip_before_filter :require_login, only: [:new, :create]
  ...
end

This can also happen with other authentication gems, such as devise, or clearance. However I did not test if they automatically skip authentication filter for creating new session.

Hey there!

My name is Patrik Bóna and I am the only programmer at Memberful. This blog is kind of dead, but I just started my own Ruby on Rails screencast. Follow me on Twitter if you want to be notified about my newest videos.