Search code examples
ruby-on-railsrubyrakebasic-authentication

Ruby on rails: how to exclude certain path from rack middleware authentication?


I am trying to use rack middleware authentication. I want to exclude certain path from the authentication. Is it possible to exclude some specific path?

This will authenticate all the routes starts with home.

def call(env)
  request = Rack::Request.new(env)
  if request.path =~ /^\/home/
    super
  else
    @app.call(env)
  end
end

I want that the path "home/users/" should be excluded from the authentication. All other path starting from "home/" should be authenticate. Any lead please, thanks.


Solution

  • If you want to exclude only "home/users/" path then you middleware should have following structure,

    def call(env)
      request = Rack::Request.new(env)
      return @app.call(env) if request.path == "home/users/"
      # your middleware logic of authentication here.
    end
    

    For more information of rack, you can refer this.