Praful Patel
Praful Patel

Reputation: 105

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.

Upvotes: 0

Views: 781

Answers (1)

Krupa Suthar
Krupa Suthar

Reputation: 680

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.

Upvotes: 1

Related Questions