0
0

More than 3 years have passed since last update.

コントローラーに記述するセキュリティの記述

Posted at

はじめに

 コントローラーに記述する最低限のセキュリティについて、忘れないように記録しておく。

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
0
0
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
0
0