Simple authentication for Sinatra in Rails Metal
Using Sinatra:Base to build Rails Metal is a joy and really speeds up the process.
I needed to protect API access with simple authentication and after trying a few routes I found this technique:
before do
if request.env['PATH_INFO'].include?('/api/cool_stuff')
Rack::Auth::Basic.new(request, 'API Access') do |username, password|
Account.authenticate(username, password)
end
end
end
The trick was keeping the authentication from triggering for requests not meant for this path.


