Skip to content

Commit

Permalink
Merge pull request #165 from luin/auth-error
Browse files Browse the repository at this point in the history
Emit auth error for wrong password. Close #164
  • Loading branch information
luin committed Oct 2, 2015
2 parents 1d36168 + a8399fc commit 03acced
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ error | client will emit `error` when an error occurs while connecting.<br>Ho
close | client will emit `close` when an established Redis server connection has closed.
reconnecting | client will emit `reconnecting` after `close` when a reconnection will be made. The argument of the event is the time (in ms) before reconnecting.
end | client will emit `end` after `close` when no more reconnections will be made.
authError | client will emit `authError` when the password specified in the options is wrong or the server doesn't require a password.

You can also check out the `Redis#status` property to get the current connection status.

Expand Down
6 changes: 3 additions & 3 deletions lib/redis/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ exports.connectHandler = function (self) {

// AUTH command should be processed before any other commands
if (self.condition.auth) {
self.auth(self.condition.auth, function (err, res) {
if (err && err.message.match('no password is set')) {
console.warn('`auth` is specified in the client but not in the server.');
self.auth(self.condition.auth, function (err) {
if (err) {
self.emit('authError', err);
}
});
}
Expand Down
23 changes: 19 additions & 4 deletions test/functional/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,33 @@ describe('auth', function () {
});
});

it('should warn when the server doesn\'t need auth', function (done) {
stub(console, 'warn', function () {
console.warn.restore();
it('should emit "authError" when the server doesn\'t need auth', function (done) {
var server = new MockServer(17379, function (argv) {
if (argv[0] === 'auth' && argv[1] === 'pass') {
return new Error('ERR Client sent AUTH, but no password is set');
}
});
var redis = new Redis({ port: 17379, password: 'pass' });
redis.on('authError', function (error) {
expect(error).to.have.property('message', 'ERR Client sent AUTH, but no password is set');
redis.disconnect();
server.disconnect();
done();
});
});

it('should emit "authError" when the password is wrong', function (done) {
var server = new MockServer(17379, function (argv) {
if (argv[0] === 'auth' && argv[1] === 'pass') {
return new Error('ERR Client sent AUTH, but no password is set');
return new Error('ERR invalid password');
}
});
var redis = new Redis({ port: 17379, password: 'pass' });
redis.on('authError', function (error) {
expect(error).to.have.property('message', 'ERR invalid password');
redis.disconnect();
server.disconnect();
done();
});
});
});

0 comments on commit 03acced

Please sign in to comment.