Search code examples
rubysinatra

Before filter in Sinatra causing a redirect loop


I have this simple code:

require 'sinatra'

before  do
  redirect '/login'
end

get '/login' do
  'hello'
end

get '/test' do
  'should not show'
end

This simple app is supposed to redirect every route, including /test to the login route. Instead, I'm getting a redirect loop.

I use the latest version of Sinatra 2.0.5.


Solution

  • You need to exclude /login route from before_filter

    before do
      redirect '/login' if request.path_info != "/login"
    end