Skip to content

Commit

Permalink
Merge pull request thelounge#437 from williamboman/fix/save-token
Browse files Browse the repository at this point in the history
src/client: fix storing updated token, ensure client config is always an object
  • Loading branch information
astorije authored Jul 1, 2016
2 parents f96027f + 7483667 commit 5bf5c0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
41 changes: 21 additions & 20 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ var inputs = [
}, {});

function Client(manager, name, config) {
if (typeof config !== "object") {
config = {};
}
_.merge(this, {
activeChannel: -1,
config: config,
Expand All @@ -67,31 +70,30 @@ function Client(manager, name, config) {

var client = this;

if (config) {
if (!config.token) {
client.updateToken(function() {
client.manager.updateUser(client.name, {token: config.token});
});
}

var delay = 0;
(config.networks || []).forEach(function(n) {
setTimeout(function() {
client.connect(n);
}, delay);
delay += 1000;
if (client.name && !client.config.token) {
client.updateToken(function(token) {
client.manager.updateUser(client.name, {token: token});
});
}

var delay = 0;
(client.config.networks || []).forEach(function(n) {
setTimeout(function() {
client.connect(n);
}, delay);
delay += 1000;
});

log.info("User '" + name + "' loaded");
if (client.name) {
log.info("User '" + client.name + "' loaded");
}
}

Client.prototype.emit = function(event, data) {
if (this.sockets !== null) {
this.sockets.in(this.id).emit(event, data);
}
var config = this.config || {};
if (config.log === true) {
if (this.config.log === true) {
if (event === "msg") {
var target = this.find(data.chan);
if (target) {
Expand Down Expand Up @@ -245,17 +247,16 @@ Client.prototype.updateToken = function(callback) {
var client = this;

crypto.randomBytes(48, function(err, buf) {
client.config.token = buf.toString("hex");
callback();
callback(client.config.token = buf.toString("hex"));
});
};

Client.prototype.setPassword = function(hash, callback) {
var client = this;

client.updateToken(function() {
client.updateToken(function(token) {
client.manager.updateUser(client.name, {
token: client.config.token,
token: token,
password: hash
});

Expand Down
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function init(socket, client) {
socket.emit("init", {
active: client.activeChannel,
networks: client.networks,
token: client.config ? client.config.token : null
token: client.config.token || null
});
}
}
Expand Down

0 comments on commit 5bf5c0e

Please sign in to comment.