HTTP basic auth for Rack :: Static application on Heroku

I have a simple Rack app hosted on Heroku. config.ru:

use Rack::Static, :urls => ["/stylesheets", "/images", "/javascripts"], :root => "public" run lambda { |env| [ 200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY) ] } 

How can I add HTTP Basic Auth? Bonus points if they work only in a working environment.

thanks

+8
ruby heroku rack
source share
2 answers
 use Rack::Static, :urls => ["/stylesheets", "/images", "/javascripts"], :root => "public" #SOLUTION: use Rack::Auth::Basic, "Restricted Area" do |username, password| [username, password] == ['admin', 'admin'] end run lambda { |env| [ 200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY) ] } 
+14
source

If you also want to protect images, stylesheets and javascripts behind basic auth, you need to install Rack :: Auth :: Basic first:

 use Rack::Auth::Basic, "Restricted Area" do |username, password| [username, password] == ['admin', 'admin'] end use Rack::Static, :urls => ["/stylesheets", "/images", "/javascripts"], :root => "public" run lambda { |env| [ 200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY) ] } 
+5
source

All Articles