3 つのモデル (顧客、ポイント、管理者) を持つアプリを構築しています。お客様にはポイントがあり、ポイントはお客様のものです。次に、管理者は属性として user_name と password_hash を持ち、Bcrypt を介してパスワードを保存します。顧客が電話番号で自分自身を検索すると、ポイントが表示されます。ただし、ポイントを追加するには、管理者がパスワード (4 桁のコード) だけでログインし、ポイントを追加するためのアクセス権を取得する必要があります。
user_name と password ではなく、パスワードだけで管理者を見つける方法に問題があります。
class AdminsController < ApplicationController
def new
@admin = Admin.new
end
def create
@Admin = Admin.new(admin_params)
if @admin.save
redirect_to root_path
else
flash[:error] = "incorrect data, please check form"
render new_admin_path
end
end
def login
@customer = Customer.find(params[:id])
# Need to get the input password
params[:password]
# Change the inputed password into a password hash
# inputed_password_hash (NEED HELP HERE)
# Compare the password hash with password hashes in the Admin model/database
# to see if it exists.
# if true, send to add points page
# if false, send back to customer page
if Admin.find_by(password_hash: inputed_password_hash)
redirect_to new_points_path
else
render customer_path
end
end
private
def admin_params
params.require(:admin).permit(:user_name, :password, :password_confirmation)
end
end