From eb47d2b06184cb43c62bf0bc601ce9076cea260a Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Wed, 14 Jun 2017 23:52:33 -0600 Subject: [PATCH 1/4] =?UTF-8?q?Return=20Invalid=20login=20message=20when?= =?UTF-8?q?=20user=20doesn=E2=80=99t=20exist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Includes failing test. Fixes https://github.com/feathersjs/feathers-authentication-local/issues/10#issuecomment-308629062 once the test is fixed --- src/verifier.js | 2 +- test/verifier.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/verifier.js b/src/verifier.js index f09d998..433f050 100644 --- a/src/verifier.js +++ b/src/verifier.js @@ -78,7 +78,7 @@ class LocalVerifier { const payload = { [`${this.options.entity}Id`]: id }; done(null, entity, payload); }) - .catch(error => error ? done(error) : done(null, error)); + .catch(error => error ? done(error) : done(null, error, { message: 'Invalid login' })); } } diff --git a/test/verifier.test.js b/test/verifier.test.js index 39a9370..458db87 100644 --- a/test/verifier.test.js +++ b/test/verifier.test.js @@ -161,6 +161,13 @@ describe('Verifier', () => { }); }); + it('produces an error message when the user did not exist', done => { + verifier.verify({}, 'nonexistinguser@gmail.com', 'admin', (err, user, info) => { + expect(info.message).to.equal('Invalid login'); + done(); + }); + }); + it('calls _comparePassword', done => { sinon.spy(verifier, '_comparePassword'); verifier.verify({}, user.email, 'admin', () => { From b0ee2ce8b35f77b9fe1b867101d94b0e471a98f3 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Wed, 21 Jun 2017 23:57:35 -0600 Subject: [PATCH 2/4] Add .vscode folder to ignores --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 936015b..2212d2f 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,5 @@ node_modules lib/ # Yarn lockfile -yarn.lock \ No newline at end of file +yarn.lock +/.vscode From 7260a660b3e163408e890c615c3974f8a1df7107 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Wed, 21 Jun 2017 23:58:15 -0600 Subject: [PATCH 3/4] =?UTF-8?q?Add=20a=20useful=20debug=20message=20when?= =?UTF-8?q?=20user=20doesn=E2=80=99t=20exist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/verifier.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/verifier.js b/src/verifier.js index 433f050..ac4e597 100644 --- a/src/verifier.js +++ b/src/verifier.js @@ -71,7 +71,13 @@ class LocalVerifier { // Look up the entity this.service.find({ query }) - .then(this._normalizeResult) + .then(response => { + const results = response.data || response + if (!results.length) { + debug(`a record with ${this.options.usernameField} of '${username}' did not exist`); + } + return this._normalizeResult(response) + }) .then(entity => this._comparePassword(entity, password)) .then(entity => { const id = entity[this.service.id]; From 39976547b8915cbe84d3992ed0cec865ed3d8f85 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Wed, 21 Jun 2017 23:58:26 -0600 Subject: [PATCH 4/4] Update the sinon stub to handle more cases. --- test/verifier.test.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test/verifier.test.js b/test/verifier.test.js index 458db87..0ff55a0 100644 --- a/test/verifier.test.js +++ b/test/verifier.test.js @@ -25,9 +25,19 @@ describe('Verifier', () => { }; service = { - find: sinon.stub().returns(Promise.resolve([user])) + find () {} }; + sinon.stub(service, "find", function (params) { + return new Promise((resolve, reject) => { + const { email } = params && params.query + if (email === 'nonexistinguser@gmail.com') { + return resolve([]) + } + return resolve([user]) + }) + }); + app.use('users', service) .configure(authentication({ secret: 'supersecret' })); @@ -185,7 +195,7 @@ describe('Verifier', () => { }); }); - it('handles false rejections in promise chain', () => { + it('handles false rejections in promise chain', (done) => { verifier._normalizeResult = () => Promise.reject(false); verifier.verify({}, user.email, 'admin', (error, entity) => { expect(error).to.equal(null); @@ -194,7 +204,7 @@ describe('Verifier', () => { }); }); - it('returns errors', () => { + it('returns errors', (done) => { const authError = new Error('An error'); verifier._normalizeResult = () => Promise.reject(authError); verifier.verify({}, user.email, 'admin', (error, entity) => {