You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
Unify auth handling
This commit is contained in:
58
index.js
58
index.js
@@ -188,8 +188,8 @@ RedisClient.prototype.create_stream = function () {
|
||||
}
|
||||
|
||||
// Fire the command before redis is connected to be sure it's the first fired command
|
||||
if (typeof this.auth_pass === 'string') {
|
||||
this.do_auth();
|
||||
if (this.auth_pass !== undefined) {
|
||||
this.auth(this.auth_pass);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -274,29 +274,6 @@ RedisClient.prototype.on_error = function (err) {
|
||||
this.connection_gone('error');
|
||||
};
|
||||
|
||||
var noPasswordIsSet = /no password is set/;
|
||||
|
||||
RedisClient.prototype.do_auth = function () {
|
||||
var self = this;
|
||||
debug('Sending auth to ' + self.address + ' id ' + self.connection_id);
|
||||
|
||||
this.send_anyway = true;
|
||||
this.send_command('auth', [this.auth_pass], function (err, res) {
|
||||
if (err) {
|
||||
if (noPasswordIsSet.test(err.message)) {
|
||||
debug('Warning: Redis server does not require a password, but a password was supplied.');
|
||||
err = null;
|
||||
res = 'OK';
|
||||
} else {
|
||||
self.emit('error', err);
|
||||
}
|
||||
} else {
|
||||
debug('Auth succeeded ' + self.address + ' id ' + self.connection_id);
|
||||
}
|
||||
});
|
||||
this.send_anyway = false;
|
||||
};
|
||||
|
||||
RedisClient.prototype.on_connect = function () {
|
||||
debug('Stream connected ' + this.address + ' id ' + this.connection_id);
|
||||
|
||||
@@ -1034,18 +1011,29 @@ RedisClient.prototype.callback_emit_error = function (callback, err) {
|
||||
}
|
||||
};
|
||||
|
||||
// Stash auth for connect and reconnect. Send immediately if already connected.
|
||||
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callback) {
|
||||
if (typeof pass !== 'string') {
|
||||
var err = new Error('The password has to be of type "string"');
|
||||
err.command = 'AUTH';
|
||||
this.callback_emit_error(callback, err);
|
||||
return true;
|
||||
}
|
||||
var noPasswordIsSet = /no password is set/;
|
||||
|
||||
RedisClient.prototype.auth = function (pass, callback) {
|
||||
var self = this;
|
||||
debug('Sending auth to ' + self.address + ' id ' + self.connection_id);
|
||||
|
||||
// Stash auth for connect and reconnect.
|
||||
this.auth_pass = pass;
|
||||
debug('Saving auth as ' + this.auth_pass);
|
||||
this.send_anyway = true;
|
||||
var tmp = this.send_command('auth', [pass], callback);
|
||||
var tmp = this.send_command('auth', [pass], function (err, res) {
|
||||
if (err) {
|
||||
if (noPasswordIsSet.test(err.message)) {
|
||||
self.warn('Warning: Redis server does not require a password, but a password was supplied.');
|
||||
err = null;
|
||||
res = 'OK';
|
||||
} else if (!callback) {
|
||||
self.emit('error', err);
|
||||
}
|
||||
}
|
||||
if (callback) {
|
||||
callback(err, res);
|
||||
}
|
||||
});
|
||||
this.send_anyway = false;
|
||||
return tmp;
|
||||
};
|
||||
|
Reference in New Issue
Block a user