Bcrypt and Rails finding an admin user just by entering password -
i'm building app has 3 models (customer, points, admin). customer has points, points belong customer. admin has user_name , password_hash attributes, storing passwords via bcrypt. once customer searches via phone number, points show up. add points, admin has log in password (code of 4 digits) access adding points.
i'm having trouble how find admin via password, not 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 input password params[:password] # change inputed password password hash # inputed_password_hash (need here) # compare password hash password hashes in admin model/database # see if exists. # if true, send add points page # if false, send 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
Comments
Post a Comment