如何在 Rails 中使用 password_digest 和表单更新用户密码

Con*_*nor 3 ruby-on-rails bcrypt ruby-on-rails-6

我有以下文件来更新用户详细信息。它们目前适用于除密码之外的所有内容。所有的逻辑都正常工作,我曾经puts检查过是否输入了正确的逻辑部分,但是似乎密码在最后@user.update(user_params)被调用时永远不会更新。

为了让它工作,我必须将下面的 2 行添加到 UserController 的密码逻辑中,这似乎不合逻辑,因为我@user.update(user_params)进一步调用应该使用提供的用户参数更新用户。任何建议、建议或意见将不胜感激。

@user.password = params[:user][:password]
@user.save
Run Code Online (Sandbox Code Playgroud)

我没有使用任何 Gems 等进行身份验证,我的用户表如下所示

create_table "users", force: :cascade do |t|
    t.string "username"
    t.string "password_digest"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "title"
    t.string "firstName"
    t.string "surname"
    t.index ["username"], name: "index_users_on_username", unique: true
end
Run Code Online (Sandbox Code Playgroud)

用户控制器

class UsersController < ApplicationController

  skip_before_action :authorized, only: [:new, :create]
  before_action :set_user, only: [:update]
  def new
    @user = User.new
  end

  def create
    @user = User.create(params.require(:user).permit(:username, :password))
    session[:user_id] = @user.id
    redirect_to '/welcome'
  end

  def update
    errorMessage = ''
    if current_user.username != params[:user][:username]
      if User.find_by(username: params[:user][:username]).nil?
      else
        errorMessage += "Username is already taken"
      end
    else
    end
    if !params[:user][:password].blank?
      if @user.authenticate(params[:user][:password_confirmation])
      else
        errorMessage += "Confirmation password is incorrect"
      end
    else
    end

    if !errorMessage.blank?
      redirect_to account_path, notice: errorMessage
    else
      @user.update(user_params)
      redirect_to account_path
    end
  end

  private 
  def user_params
    params.require(:user).permit(:username, :title, :firstName, :surname,:password, :password_confirmation)
  end
  def set_user
    @user = current_user
  end
end
Run Code Online (Sandbox Code Playgroud)

编辑.html.erb

<p id=”notice”><%= notice %></p>
<%= form_for current_user do |f|%>
    <div class="form-group row col-md-12">
        <%= f.label :username, class:"col-sm-2 col-form-label"%><br>
        <%= f.text_field :username, class:"form-control col-sm-10" %>
    </div>

    <div class="form-group row col-md-12">
        <div class="col-md-2">
            <%= f.label :title%><br>
            <%= f.text_field :title, class:"form-control" %>
        </div>
        <div class="col-md-5">
            <%= f.label :firstName, "First Name" %><br>
            <%= f.text_field :firstName, class:"form-control" %>
        </div>
        <div class="col-md-5">
            <%= f.label :surname %><br>
            <%= f.text_field :surname, class:"form-control" %>
        </div>
    </div>

    <div class="form-group row col-md-12">
        <div class="col-md-6">
            <%= f.label :password_confirmation, "Current Password"%><br>
            <%= f.password_field :password_confirmation, class:"form-control" %>
        </div>
        <div class="col-md-6">
            <%= f.label :password, "New Password"%><br>
            <%= f.password_field :password, class:"form-control" %>
        </div>
        <small id="passwordHelpBlock" class="form-text text-muted col-md-12">
            To update your password, please confirm your current password.
        </small>
    </div>

    <%= f.submit "Submit" ,class: "btn btn-primary"%>
<% end %>
Run Code Online (Sandbox Code Playgroud)

用户模型

class User < ApplicationRecord
    has_secure_password
end
Run Code Online (Sandbox Code Playgroud)

Chr*_*ian 5

您的控制器有点复杂,我认为这以某种方式导致了您的问题。您应该在视图中输出错误以查看出了什么问题,不幸的是,这不是您问题的一部分。

尽管如此,一般来说,有两个选项可以在bcrypt ruby​​ Github 存储库中进行描述。请注意,我还没有尝试使用current_user,params[:user]@user.authenticate消除复杂性并专注于 bcrypt 主题。

选项1

使用has_secure_password在类似这样的用户模式:

class User < ApplicationRecord
  has_secure_password
end
Run Code Online (Sandbox Code Playgroud)

您需要一个密码摘要列,并且需要:password在 user_params 方法中使用。

def user_params
  params.require(:user).permit(:username, :title, :firstName, :surname, :password, :password_confirmation)
end
Run Code Online (Sandbox Code Playgroud)

输入的密码将保存在数据库中的password_digest 列中。

选项 2

在 bcrypt Github 页面the-user-model 部分进行了描述

在这种情况下,您不使用has_secure_password而是将以下代码添加到您的用户模型中:

class User < ApplicationRecord
  include BCrypt

  def password
    @password ||= Password.new(password_hash)
  end

  def password=(new_password)
    @password = Password.create(new_password)
    self.password_hash = @password
  end
end
Run Code Online (Sandbox Code Playgroud)

然后你的user_params方法需要反映:password_hash属性名称:

def user_params
  params.require(:user).permit(:username, :title, :firstName, :surname, :password_hash, :password_confirmation)
end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您的数据库将需要一个password_hash列,而不是password_digest使用上面的示例代码使其工作。当然,你也可以重用password_digest而不是password_hash,然后password_hash在上面的代码中替换为password_digest

笔记

在这两种情况下,我都像使用password字段一样使用视图:

<div class="form-group row col-md-12">
  <div class="col-md-6">
    <%= f.label :password, "New Password"%><br>
      <%= f.password_field :password, class:"form-control" %>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我的测试中的控制器如下所示:

def update
  if @user.update(user_params)
    redirect_to @user, notice: 'User was successfully updated.'
  else
    render :edit
  end
end
Run Code Online (Sandbox Code Playgroud)

测试应用

我还使用选项 1 将这个小测试 Rails 应用程序添加has_secure_password到 Github 存储库 @ https://github.com/cadamini/rails-bcrypt-ruby-test