When creating an app and publishing it online, there are times when you don’t want others to see it, or due to company policies, you can’t allow anyone to access even a prototype. In such cases, Rails provides a system that allows you to easily implement security.
The method to use is http_basic_authenticate_with
.
For example, to prevent unauthorized users from posting to the BlogsController, you can block them by setting a name
and password
. You can also create exceptions, such as allowing free access to the index action.
This is useful when you don’t need to go as far as creating a full login system.
class BlogsController < ApplicationController
http_basic_authenticate_with name: "tanaka", password: "password", except: [:index]