您不能得到舊密碼,因爲它被哈希,但你可以檢查它是否正確,如果你有它的明文。
您可以使用Accounts._checkPassword(user, password)
方法來檢查舊密碼是否正確。它執行here。
user
應該是用戶對象,password
應該是純文本密碼字符串。
如果結果(它是一個對象)不包含error
屬性,那麼密碼是正確的。
您還可以在用於處理Accounts.changePassword(oldPassword, newPassword, [callback])
呼叫的方法的implementation上觀看(獲取靈感),該呼叫將更改當前用戶的密碼。
如果您不希望將明文密碼發送到服務器(這是一般最好不要發送純文本版本),你可以散列它使用SHA256的客戶端上,通過將其到Accounts._hashPassword(plainTextPassword)
(實施here,在accounts-password
包中)。
// 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' }
你的「明文」是指密碼輸入的類型應爲「文本」,而不是密碼? – sana
不,我的意思是你需要一個包含密碼的字符串,或者一個包含sha-256版本的對象(通過'SHA256(password)'對密碼串進行散列處理的結果)。 – MasterAM
然後我應該添加任何特定的包? – sana