我正在开发一个Rails 3.0应用程序,并使用OmniAuth + Identity进行身份验证注册。我已经实现了一个通过外键与Identity模型绑定的User模型,一切运行良好。现在,我想实现忘记密码功能。
根据用户的电子邮件,我想向他们发送一封电子邮件,其中包含重置密码的链接。该电子邮件包含与用户关联的随机十六进制字符串。
现在,如何重置用户的身份密码?
在Identity数据库表中,它存储为password_digest。我可以覆盖这个吗?
答案 0 :(得分:10)
这样做:
@identity = Identity.find(1)
@identity.password = "newpassword"
@identity.password_confirmation = "newpassword"
@identity.save
在omniauth-identity的issue中,wdspkr说:
一旦您了解omniauth-identity使用的是ActiveModel SecurePassword很容易解决这个问题。而不是设置 password_digest你刚设置密码和password_confirmation和 更新
答案 1 :(得分:4)
事实证明就是这么简单。只需覆盖Identity表中的现有password_digest即可。使用BCrypt库创建password_digest:
require 'bcrypt'
...
class UsersController < ApplicationController
...
def update
@user = User.find(params[:id])
...
user_identity = Identity.find_by_email(@user.email)
unencrypted_password = params[:user][:password].to_s
password_digest = BCrypt::Password.create(unencrypted_password)
user_identity.password_digest = password_digest;
user_identity.save!
end
end