Web Development in Ruby : HTTP Basic Authentication in Rack App
To learn how to use http basic authentication in your Rack app.
Let's now run the example that comes with Rack distribution. Create lobster.rb:
require 'rack' require 'thin' require 'rack/lobster' Rack::Handler::Thin.run Rack::Lobster.new, Port: 3222
http://localhost:3222 to view the lobster.
Change the lobster.rb as follows:
require 'rack' require 'thin' require 'rack/lobster' lobster = Rack::Lobster.new protected_lobster = Rack::Auth::Basic.new(lobster) do |username, password| 'secret' == password end protected_lobster.realm = 'Lobster 2.0' pretty_protected_lobster = Rack::ShowStatus.new(Rack::ShowExceptions.new(protected_lobster)) Rack::Handler::Thin.run pretty_protected_lobster, Port: 3222
http://localhost:3222. You are now prompted with http basic authentication popup.
Enter anything for the username, secret for the password. You can now see the protected lobster.
In this article, you learned how to use basic authentication in a Rack app.
Ace the Technical Interview
- Easily find the gaps in your knowledge
- Get customized lessons based on where you are
- Take consistent action everyday
- Builtin accountability to keep you on track
- You will solve bigger problems over time
- Get the job of your dreams
Take the 30 Day Coding Skills Challenge
Gain confidence to attend the interview