我已经尝试了很多方法,但似乎BCrypt正在加密用户提交的密码两次。无法使用Bcrypt更新密码
当用户注册时 - Bcrypt很好用,而且我可以登录。但是当我尝试更新密码时,我的密码已经无法登录。我的数据库显示密码正在更新和散列,但我无法登录。
我甚至删除了线@customer.save
,但我的数据库仍显示密码正在更新中!
是什么东西在引擎盖下更新我不知道?见relatd SO螺纹: Updating password with BCrypt
在我Customer.rb
require 'bcrypt'
class Customer < ActiveRecord::Base
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
def self.authenticate(email, password)
@customer = Customer.find_by_email(email)
if @customer && @customer.password == password
return @customer
else
return nil
end
end
end
在我customer_controller中,创建一个实际工作
require 'bcrypt'
class CustomersController < ApplicationController
def create_customer_account_iphone
@customer_count = Customer.where(email: params[:email]).size rescue nil
if(@customer_count == 0 || @customer_count == nil ||)
@customer = Customer.new(first_name: params[:first_name], email: params[:email])
@customer.password = params[:password] //this calls my model methods
@customer.save //here I am saving
unless ([email protected])
respond_to do |format|
msg = {:status => "SUCCESS", :messages => "Customer created", :data => @customer.as_json}
format.json { render :json => msg } # don't do msg.to_json
end
else
respond_to do |format|
msg = {:status => "FAILED", :messages => "Customer Not Saved"}
format.json { render :json => msg } # don't do msg.to_json
end
end
def sign_in_iphone
@customer = Customer.authenticate(params[:email], params[:password])
unless (@customer == 0 || @customer == nil)
respond_to do |format|
msg = {:status => "SUCCESS", :message => "CUSTOMER", :data => @customer.as_json}
format.json { render :json => msg } # don't do msg.to_json
end
else
respond_to do |format|
msg = {:status => "FAILED"}
format.json { render :json => msg } # don't do msg.to_json
end
end
end
在我password_reset_controller代码
class CustomerPasswordResetsController < ApplicationController
def edit
@customer = Customer.find_by_password_reset_token!(params[:id])
end
def update
@customer = Customer.find_by_password_reset_token!(params[:id])
if @customer.password_reset_sent_at < 2.hours.ago
redirect_to new_customer_password_reset_path, :alert => "Password reset has expired."
else
@customer.password_hash = BCrypt::Password.create(params[:password])
# @customer.save
unless [email protected]
redirect_to new_customer_password_reset_path, :alert => "Password has been reset!"
else
render :edit
end
end
end
在我password_reset.html.erb
<%= form_for @customer, :url => customer_password_reset_path(params[:id]), :method => :patch do |f| %>
<% if @customer.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @customer.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Update Password" %></div>
尝试'@ customer.update_attributes(:password_hash,BCrypt :: Password.create(params [:password]))' – christoshrousis 2015-02-12 04:47:39
您的模型代码中是否存在涉及此字段的内容? – tadman 2015-02-12 04:56:57
@tadman你能更具体吗?在我的模型中有一些东西,实际上我会发帖 – DaynaJuliana 2015-02-12 04:59:24