LoginSignup
1
1

More than 3 years have passed since last update.

ユーザー認証の導入

Posted at

ユーザー認証の導入

Basic認証

HTTP通信の規格に備え付けられている、ユーザー認証の仕組みのこと
d5d42271f315066cae86af0e91d3b5cc.png

RailsアプリケーションにBasic認証を導入

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :basic_auth
  (省略)

  private

  def basic_auth
    authenticate_or_request_with_http_basic do |username, password|
      username == 'admin' && password == '1111'
    end
  end
end

開発環境の環境変数に、ユーザー名とパスワードを設定

macOSがCatalina以降

ターミナル
% vim ~/.zshrc

# .zshrcを開いたら、「iとタイプしてインサートモードに移行

# .zshrcの内部に次の記述を追加
export BASIC_AUTH_USER='admin'
export BASIC_AUTH_PASSWORD='2222'
# 記述を追加したらescキーを押してインサートモードを抜け :wqと入力して保存して終了する

# .zshrcを再読み込みし定義した環境変数を有効にする
% source ~/.zshrc

macOSがMojave以前の方

ターミナル
$ vim ~/.bash_profile

# .bash_profileを開いたら、「iとタイプしてインサートモードに移行

# .bash_profileの内部に次の記述を追加
export BASIC_AUTH_USER='admin'
export BASIC_AUTH_PASSWORD='2222'
# 記述を追加したらescキーを押してインサートモードを抜け :wqと入力して保存して終了する

# .bash_profileを再読み込みし定義した環境変数を有効にする
$ source ~/.bash_profile
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