作成しているアプリケーションでbasic認証を使ってたので、備忘録として記載
gemのinstall
gemfileに下記を追加
gem 'dotenv-rails'
追加したら、bundle installを行う
環境変数の追加
gemfileやappなどがあるディレクトリに.envファイルを作成
.env
BASIC_AUTH_USER='user名'
BASIC_AUTH_PASSWORD='password'
controllerの設定
controller.rb
before_action :basic_auth
private
def basic_auth
authenticate_or_request_with_http_basic do |username, password|
username == ENV["BASIC_AUTH_USER"] && password == ENV["BASIC_AUTH_PASSWORD"]
end
end