This article will discuss HTTP Basic Authentication in Ruby on Rails. This can be useful when a page needs to be accessible only to users with a password.
First, create a new application.
1
2
rails new basic_auth
rails db:create db:migrate
Now generate a controller.
1
rails g controller Welcome index show
Now, in app/controllers/welcome_controller.rb
, put the following code:
1
2
3
4
5
6
7
8
9
class WelcomeController < ApplicationController
http_basic_authenticate_with name: "user", password: "abcdef", only: :show
def index; end
def show
end
end
Now run the Rails server and see how it works.
There you go! Happy authenticating!