はじめに
コントローラーに記述する最低限のセキュリティについて、忘れないように記録しておく。
Basic認証
app/controllers/application_controller.rb
before_action :basic_auth
private
def basic_auth
authenticate_or_request_with_http_basic do |username, password|
username == 'ユーザー名' && password == 'password'
end
end
直接URLを入力されてもリダイレクトさせる
before_action :authenticate_user! #←ログイン状態でないときはサインインページに遷移
before_action :move_to_index
private
def move_to_index
@item = Item.find(params[:item_id])
redirect_to root_path if user_signed_in? && current_user.id == @item.user_id
end