From c5692f8b137e95f4a00b6cb880c71107cc42bf50 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 17 Mar 2016 15:25:21 -0400 Subject: [PATCH] Only throw "Invalid 2FA code" if we're sure that's the cause Currently, such error will be thrown when `resin.auth.twoFactor.challenge()` rejects, but an invalid code is not the only thing this function can reject for. --- build/utils/patterns.js | 7 +++++-- lib/utils/patterns.coffee | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/build/utils/patterns.js b/build/utils/patterns.js index 46a10bb5..6a79100c 100644 --- a/build/utils/patterns.js +++ b/build/utils/patterns.js @@ -56,9 +56,12 @@ limitations under the License. message: 'Two factor auth challenge:', name: 'code', type: 'input' - }).then(resin.auth.twoFactor.challenge)["catch"](function() { + }).then(resin.auth.twoFactor.challenge)["catch"](function(error) { return resin.auth.logout().then(function() { - throw new Error('Invalid two factor authentication code'); + if (error.name === 'ResinRequestError' && error.statusCode === 401) { + throw new Error('Invalid two factor authentication code'); + } + throw error; }); }); }); diff --git a/lib/utils/patterns.coffee b/lib/utils/patterns.coffee index d9592aea..e83fce6d 100644 --- a/lib/utils/patterns.coffee +++ b/lib/utils/patterns.coffee @@ -44,9 +44,11 @@ exports.authenticate = (options) -> name: 'code' type: 'input' .then(resin.auth.twoFactor.challenge) - .catch -> + .catch (error) -> resin.auth.logout().then -> - throw new Error('Invalid two factor authentication code') + if error.name is 'ResinRequestError' and error.statusCode is 401 + throw new Error('Invalid two factor authentication code') + throw error exports.askLoginType = -> return form.ask