在我的 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);
}
}
我想知道旧密码是否正确。
最佳答案
您不能得到旧密码,因为它是散列的,但如果您有明文形式的密码,您可以检查它是否正确。
您可以使用 Accounts._checkPassword(user, password)
方法来检查旧密码是否正确。已实现 here .
user
应该是用户对象,password
应该是纯文本密码字符串。
如果结果(它是一个对象)不包含 error
属性,那么密码是正确的。
您还可以在 implementation 查看(以获取灵感)用于处理对 Accounts.changePassword(oldPassword, newPassword, [callback])
的调用的方法,它会更改当前用户的密码。
如果您不想将纯文本密码发送到服务器(通常最好不要发送纯文本版本),您可以在客户端使用 SHA256 对其进行哈希处理,方法是将其传递给 Accounts._hashPassword(plainTextPassword)
(在 accounts-password
包中实现了 here)。
// on the client
>>> Accounts._hashPassword("foobar");
{
"digest":"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
"algorithm":"sha-256"
}
使用此函数的结果调用您的服务器方法。假设您在方法中将 SHA256 散列密码作为 oldPassword
:
//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' }
关于javascript - 在 meteor JS 中检查用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146797/