diff --git a/client/index.js b/client/index.js index 54c9b49f..a2a31211 100644 --- a/client/index.js +++ b/client/index.js @@ -568,19 +568,30 @@ Client.prototype.reforgotPassword = function (callback) { } } -Client.prototype.resetPassword = function (code, password, callback) { - // this will generate a new wrapKb on the server - var wrapKb = '0000000000000000000000000000000000000000000000000000000000000000' - var p = this.setupCredentials(this.email, password) +Client.prototype.verifyPasswordResetCode = function (code, callback) { + var p = this.api.passwordForgotVerifyCode(this.forgotPasswordToken, code) .then( - function () { - return this.api.passwordForgotVerifyCode(this.forgotPasswordToken, code) + function (result) { + this.accountResetToken = result.accountResetToken }.bind(this) ) + if (callback) { + p.done(callback.bind(null, null), callback) + } + else { + return p + } +} + +Client.prototype.resetPassword = function (newPassword, callback) { + if (!this.accountResetToken) { + throw new Error("call verifyPasswordResetCode before calling resetPassword"); + } + // this will generate a new wrapKb on the server + var wrapKb = '0000000000000000000000000000000000000000000000000000000000000000' + var p = this.setupCredentials(this.email, newPassword) .then( - function (json) { - return tokens.AccountResetToken.fromHex(json.accountResetToken) - } + tokens.AccountResetToken.fromHex.bind(null, this.accountResetToken) ) .then( function (accountResetToken) { diff --git a/test/run/verification_tests.js b/test/run/verification_tests.js index c2718f00..edabfa6a 100644 --- a/test/run/verification_tests.js +++ b/test/run/verification_tests.js @@ -197,7 +197,8 @@ function main() { ) .then( function (code) { - return client.resetPassword(code, newPassword) + t.throws(function() { client.resetPassword(newPassword); }) + return resetPassword(client, code, newPassword) } ) .then( @@ -285,7 +286,7 @@ function main() { ) .then( function () { - return client.resetPassword('wrongcode', 'password') + return resetPassword(client, 'wrongcode', 'password') } ) .then( @@ -299,7 +300,7 @@ function main() { ) .then( function () { - return client.resetPassword('wrongcode', 'password') + return resetPassword(client, 'wrongcode', 'password') } ) .then( @@ -313,7 +314,7 @@ function main() { ) .then( function () { - return client.resetPassword('wrongcode', 'password') + return resetPassword(client, 'wrongcode', 'password') } ) .then( @@ -327,7 +328,7 @@ function main() { ) .then( function () { - return client.resetPassword('wrongcode', 'password') + return resetPassword(client, 'wrongcode', 'password') } ) .then( @@ -452,3 +453,11 @@ function waitLoop() { } waitLoop() + + +function resetPassword(client, code, newPassword) { + return client.verifyPasswordResetCode(code) + .then(function() { + return client.resetPassword(newPassword) + }) +}