Skip to content
6 changes: 3 additions & 3 deletions src/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ function Kuzzle (host, options, cb) {
var copy;

if (data.type === 'TokenExpired') {
return self.unsetJwt();
self.unsetJwt();
return self.emitEvent('tokenExpired');
}

if (data.type === 'document') {
Expand Down Expand Up @@ -346,6 +347,7 @@ function Kuzzle (host, options, cb) {

this.network.addListener('tokenExpired', function() {
self.unsetJwt();
self.emitEvent('tokenExpired');
});

if ((options && options.connect || 'auto') === 'auto') {
Expand Down Expand Up @@ -486,8 +488,6 @@ Kuzzle.prototype.setJwt = function(token) {
*/
Kuzzle.prototype.unsetJwt = function() {
this.jwt = undefined;
this.emitEvent('tokenExpired');

return this;
};

Expand Down
2 changes: 1 addition & 1 deletion src/networkWrapper/protocols/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class SocketIO extends RTWrapper {
*/
close() {
this.forceDisconnect = true;
this.state = 'disconnected';
this.state = 'offline';
this.socket.close();
this.socket = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/networkWrapper/protocols/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class WSNode extends RTWrapper {
* Closes the connection
*/
close () {
this.state = 'disconnected';
this.state = 'offline';
this.removeAllListeners();
this.wasConnected = false;
if (this.client) {
Expand Down
11 changes: 2 additions & 9 deletions test/kuzzle/connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@ describe('Kuzzle connect', function () {
});

it('should keep a valid JWT at reconnection', function () {
var
kuzzle = new Kuzzle('somewhereagain', {connect: 'manual'}),
eventStub = sinon.stub();
var kuzzle = new Kuzzle('somewhereagain', {connect: 'manual'});

kuzzle.checkToken = sinon.stub();

kuzzle.jwt = 'foobar';
kuzzle.addListener('tokenExpired', eventStub);

kuzzle.connect();
clock.tick();
Expand All @@ -118,18 +115,15 @@ describe('Kuzzle connect', function () {
kuzzle.checkToken.yield(null, {valid: true});

should(kuzzle.jwt).be.eql('foobar');
should(eventStub).not.be.called();
});

it('should empty the JWT at reconnection if it has expired', function () {
var
kuzzle = new Kuzzle('somewhereagain', {connect: 'manual'}),
eventStub = sinon.stub();
kuzzle = new Kuzzle('somewhereagain', {connect: 'manual'});

kuzzle.checkToken = sinon.stub();

kuzzle.jwt = 'foobar';
kuzzle.addListener('tokenExpired', eventStub);

kuzzle.connect();
clock.tick();
Expand All @@ -139,7 +133,6 @@ describe('Kuzzle connect', function () {
kuzzle.checkToken.yield(null, {valid: false});

should(kuzzle.jwt).be.undefined();
should(eventStub).be.calledOnce();
});

it('should register listeners upon receiving a "disconnect" event', function () {
Expand Down
8 changes: 0 additions & 8 deletions test/kuzzle/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,14 +983,6 @@ describe('Kuzzle methods', function () {
kuzzle.unsetJwt();
should(kuzzle.jwt).be.undefined();
});

it('should emit a "tokenExpired" event', function () {
var cb = sinon.stub();

kuzzle.addListener('tokenExpired', cb);
kuzzle.unsetJwt();
should(cb).be.calledOnce();
});
});

describe('#updateMyCredentials', function() {
Expand Down