2016-12-14 47 views
1

在我的Meteor應用程序中,我想更改另一個用戶的密碼。我想知道在更改之前是否有任何方法獲取舊密碼。在流星JS中檢查用戶密碼

這是我的服務器端方法:

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); 
    } 
} 

我想知道,如果舊密碼正確與否。

回答

2

您不能得到舊密碼,因爲它被哈希,但你可以檢查它是否正確,如果你有它的明文。

您可以使用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' } 
+0

你的「明文」是指密碼輸入的類型應爲「文本」,而不是密碼? – sana

+0

不,我的意思是你需要一個包含密碼的字符串,或者一個包含sha-256版本的對象(通過'SHA256(password)'對密碼串進行散列處理的結果)。 – MasterAM

+0

然後我應該添加任何特定的包? – sana