Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Commit 3fa1564

Browse files
Merge pull request #25 from feathersjs/better-error-message
Return Invalid login message when user doesn’t exist
2 parents df32656 + 3997654 commit 3fa1564

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ node_modules
3333
lib/
3434

3535
# Yarn lockfile
36-
yarn.lock
36+
yarn.lock
37+
/.vscode

src/verifier.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ class LocalVerifier {
7878

7979
// Look up the entity
8080
this.service.find({ query })
81-
.then(this._normalizeResult)
81+
.then(response => {
82+
const results = response.data || response
83+
if (!results.length) {
84+
debug(`a record with ${this.options.usernameField} of '${username}' did not exist`);
85+
}
86+
return this._normalizeResult(response)
87+
})
8288
.then(entity => this._comparePassword(entity, password))
8389
.then(entity => {
8490
const id = entity[this.service.id];
8591
const payload = { [`${this.options.entity}Id`]: id };
8692
done(null, entity, payload);
8793
})
88-
.catch(error => error ? done(error) : done(null, error));
94+
.catch(error => error ? done(error) : done(null, error, { message: 'Invalid login' }));
8995
}
9096
}
9197

test/verifier.test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,19 @@ describe('Verifier', () => {
2525
};
2626

2727
service = {
28-
find: sinon.stub().returns(Promise.resolve([user]))
28+
find () {}
2929
};
3030

31+
sinon.stub(service, "find", function (params) {
32+
return new Promise((resolve, reject) => {
33+
const { email } = params && params.query
34+
if (email === 'nonexistinguser@gmail.com') {
35+
return resolve([])
36+
}
37+
return resolve([user])
38+
})
39+
});
40+
3141
app.use('users', service)
3242
.configure(authentication({ secret: 'supersecret' }));
3343

@@ -204,6 +214,13 @@ describe('Verifier', () => {
204214
});
205215
});
206216

217+
it('produces an error message when the user did not exist', done => {
218+
verifier.verify({}, 'nonexistinguser@gmail.com', 'admin', (err, user, info) => {
219+
expect(info.message).to.equal('Invalid login');
220+
done();
221+
});
222+
});
223+
207224
it('calls _comparePassword', done => {
208225
sinon.spy(verifier, '_comparePassword');
209226
verifier.verify({}, user.email, 'admin', () => {
@@ -221,7 +238,7 @@ describe('Verifier', () => {
221238
});
222239
});
223240

224-
it('handles false rejections in promise chain', () => {
241+
it('handles false rejections in promise chain', (done) => {
225242
verifier._normalizeResult = () => Promise.reject(false);
226243
verifier.verify({}, user.email, 'admin', (error, entity) => {
227244
expect(error).to.equal(null);
@@ -230,7 +247,7 @@ describe('Verifier', () => {
230247
});
231248
});
232249

233-
it('returns errors', () => {
250+
it('returns errors', (done) => {
234251
const authError = new Error('An error');
235252
verifier._normalizeResult = () => Promise.reject(authError);
236253
verifier.verify({}, user.email, 'admin', (error, entity) => {

0 commit comments

Comments
 (0)