1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails Basic認証

Posted at

作成しているアプリケーションで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

確認

以上のことが終了したらrails sで起動すると、下記のような認証を求めるものが出てくる
image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?