在我的 Meteor 应用程序中,我想更改另一个用户的密码。我想知道是否有任何方法可以在更改之前获取旧密码。
这是我的服务器端方法:
updateuser(id,password, options) {
try {
Meteor.users.update({ _id: id }, { $set: options })
Accounts.setPassword(id, password, options)
}
catch (e) {
return false;
throw new Meteor.Error(500, 'updateUser error', e);
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道旧密码是否正确。
您无法获得旧密码,因为它是经过哈希处理的,但是如果您拥有明文密码,您可以检查它是否正确。
您可以使用这些Accounts._checkPassword(user, password)
方法来检查旧密码是否正确。它在这里实现。
user
应该是用户对象并且password
应该是纯文本密码字符串。
如果结果(它是一个对象)不包含error
属性,则密码是正确的。
您还可以查看(以获取灵感)用于处理对 的调用的方法的实现,该方法Accounts.changePassword(oldPassword, newPassword, [callback])
会更改当前用户的密码。
如果您不想将纯文本密码发送到服务器(通常最好不要发送纯文本版本),您可以使用 SHA256 在客户端上散列它,方法是将其传递给Accounts._hashPassword(plainTextPassword)
(在此处实现,在accounts-password
包裹)。
// on the client
>>> Accounts._hashPassword("foobar");
{
"digest":"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
"algorithm":"sha-256"
}
Run Code Online (Sandbox Code Playgroud)
使用此函数的结果调用您的服务器方法。假设你有oldPassword
你的方法中的 SHA256 散列密码:
//on the server
const user = Meteor.users.findOne(userId);
Accounts._checkPassword(user, "wrongPassword");
// results in:
{ userId: 'theuserid',
error:
{ [Error: Incorrect password [403]]
error: 403,
reason: 'Incorrect password',
details: undefined,
message: 'Incorrect password [403]',
errorType: 'Meteor.Error' } }
Accounts._checkPassword(user, oldPassword);
// results in the following if the password is correct
{ userId: 'theuserid' }
Run Code Online (Sandbox Code Playgroud)