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

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #25 from feathersjs/better-error-message
Return Invalid login message when user doesn’t exist
  • Loading branch information
marshallswain committed Jun 22, 2017
2 parents df32656 + 3997654 commit 3fa1564
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -33,4 +33,5 @@ node_modules
lib/

# Yarn lockfile
yarn.lock
yarn.lock
/.vscode
10 changes: 8 additions & 2 deletions src/verifier.js
Expand Up @@ -78,14 +78,20 @@ 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];
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' }));
}
}

Expand Down
23 changes: 20 additions & 3 deletions test/verifier.test.js
Expand Up @@ -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' }));

Expand Down Expand Up @@ -204,6 +214,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', () => {
Expand All @@ -221,7 +238,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);
Expand All @@ -230,7 +247,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) => {
Expand Down

0 comments on commit 3fa1564

Please sign in to comment.