title: Adding HTTP Basic Authentication to some routes in routes.rb
body: <p>I want to add HTTP Basic Authentication to a some of my routes in my <code>routes.rb</code> file.</p>
  
  <p>I got the idea from <a href="http://asciicasts.com/episodes/271-resque" rel="noreferrer">http://asciicasts.com/episodes/271-resque</a></p>
  
  <p><code>routes.rb</code></p>
  
  <pre><code>Coderbits::Application.routes.draw do  
    resources :snippets  
    root :to =&gt; "snippets#new"  
    authenticate :admin do  
      mount Resque::Server, :at =&gt; "/resque"  
    end  
  end
  </code></pre>
  
  <p><code>config/initializers/resque_auth.rb</code></p>
  
  <pre><code>Resque::Server.use(Rack::Auth::Basic) do |user, password|  
    password == "secret"  
  end 
  </code></pre>
  
  <p>If I just want to protect routes that are in my rails app, what should I put in the initializer file?</p>
  
  <p>My work around for right now is to add a before filter in my application controller using if the request is not for a controller that I've whitelisted:</p>
  
  <pre><code>authenticate_or_request_with_http_basic do |user, password|
    user == ENV["ADMIN_USER"] &amp;&amp; password == ENV["ADMIN_PASS"]
  end
  </code></pre>