Skip to content

Commit 1900bf0

Browse files
author
Shiranuit
authored
Merge pull request #659 from kuzzleio/7.7.4-proposal
Release 7.7.4
2 parents b6890f7 + 7e4f883 commit 1900bf0

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "7.7.3",
3+
"version": "7.7.4",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/Kuzzle.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ export class Kuzzle extends KuzzleEventEmitter {
429429

430430
set jwt (encodedJwt) {
431431
this.auth.authenticationToken = encodedJwt;
432+
433+
this._loggedIn = encodedJwt ? true : false;
432434
}
433435

434436
get offlineQueue () {
@@ -599,7 +601,6 @@ export class Kuzzle extends KuzzleEventEmitter {
599601
// If an authenticator was set, check if a user was logged in and if the token is still valid and try
600602
// to re-authenticate if needed. Otherwise the SDK is in disconnected state.
601603
if ( this._loggedIn
602-
&& this.authenticator
603604
&& ! await this.tryReAuthenticate()
604605
) {
605606
this._loggedIn = false;
@@ -634,6 +635,14 @@ export class Kuzzle extends KuzzleEventEmitter {
634635
return true;
635636
}
636637

638+
/**
639+
* Check if there is an authenticator after verifying if the token is still valid,
640+
* like so API Keys can be used even if there is no authenticator since they will be still valid.
641+
*/
642+
if (! this.authenticator) {
643+
return false;
644+
}
645+
637646
await this.authenticate();
638647

639648
return true;
@@ -839,7 +848,7 @@ Discarded request: ${JSON.stringify(request)}`));
839848
return;
840849
}
841850

842-
if (this._loggedIn && this.authenticator && await this.tryReAuthenticate()) {
851+
if (this._loggedIn && await this.tryReAuthenticate()) {
843852
this.emit('reAuthenticated');
844853

845854
return;
@@ -854,6 +863,7 @@ Discarded request: ${JSON.stringify(request)}`));
854863

855864
this._lastTokenExpired = now;
856865

866+
this.jwt = null;
857867
this.emit('tokenExpired');
858868
}
859869

src/controllers/Realtime.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ export class RealtimeController extends BaseController {
226226
* Called when kuzzle is disconnected
227227
*/
228228
private saveSubscriptions () {
229-
for (const roomId of this._subscriptions.keys()) {
230-
for (const room of this._subscriptions.get(roomId)) {
229+
/**
230+
* Use forEach instead of iterating over Map.keys() because the Webpack
231+
* transpilation is producing bad code leading to a loop not iterating.
232+
*/
233+
this._subscriptions.forEach((rooms, roomId) => {
234+
235+
for (const room of rooms) {
231236
room.removeListeners();
232237

233238
if (room.autoResubscribe) {
@@ -239,15 +244,20 @@ export class RealtimeController extends BaseController {
239244
}
240245

241246
this._subscriptions.delete(roomId);
242-
}
247+
248+
});
243249
}
244250

245251
/**
246252
* Called on kuzzle reconnection
247253
*/
248254
private resubscribe () {
249-
for (const roomId of this._subscriptionsOff.keys()) {
250-
for (const room of this._subscriptionsOff.get(roomId)) {
255+
/**
256+
* Use forEach instead of iterating over Map.keys() because the Webpack
257+
* transpilation is producing bad code leading to a loop not iterating.
258+
*/
259+
this._subscriptionsOff.forEach((rooms, roomId) => {
260+
for (const room of rooms) {
251261
if (!this._subscriptions.has(roomId)) {
252262
this._subscriptions.set(roomId, []);
253263
}
@@ -258,18 +268,22 @@ export class RealtimeController extends BaseController {
258268
}
259269

260270
this._subscriptionsOff.delete(roomId);
261-
}
271+
});
262272
}
263273

264274
/**
265275
* Called when a token expire
266276
*/
267277
private removeSubscriptions() {
268-
for (const roomId of this._subscriptions.keys()) {
269-
for (const room of this._subscriptions.get(roomId)) {
278+
/**
279+
* Use forEach instead of iterating over Map.keys() because the Webpack
280+
* transpilation is producing bad code leading to a loop not iterating.
281+
*/
282+
this._subscriptions.forEach((rooms) => {
283+
for (const room of rooms) {
270284
room.removeListeners();
271285
}
272-
}
286+
});
273287

274288
this._subscriptions = new Map();
275289
this._subscriptionsOff = new Map();

test/kuzzle/authenticator.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const should = require('should');
22
const sinon = require('sinon');
33
const ProtocolMock = require('../mocks/protocol.mock');
44
const { Kuzzle } = require('../../src/Kuzzle');
5+
const generateJwt = require('../mocks/generateJwt.mock');
56

67
describe('Kuzzle authenticator function mecanisms', () => {
78
let kuzzle;
@@ -16,6 +17,20 @@ describe('Kuzzle authenticator function mecanisms', () => {
1617
sinon.restore();
1718
});
1819

20+
describe('jwt property', () => {
21+
it('should set the SDK property _loggedIn when setting the JWT property', () => {
22+
kuzzle.jwt = generateJwt();
23+
24+
should(kuzzle._loggedIn).be.true();
25+
});
26+
27+
it('should set the SDK property _loggedIn when setting the JWT property to null or undefined', () => {
28+
kuzzle.jwt = null;
29+
30+
should(kuzzle._loggedIn).be.false();
31+
});
32+
});
33+
1934
describe('connected listener', () => {
2035
let resolve;
2136
let promise;
@@ -230,6 +245,17 @@ describe('Kuzzle authenticator function mecanisms', () => {
230245
should(reconnectionErrorSpy).not.be.called();
231246
});
232247

248+
it('should returns false if the token is not valid and there is no authenticator', async () => {
249+
kuzzle.auth.checkToken.resolves({ valid: false });
250+
kuzzle.authenticator = null;
251+
252+
const ret = await kuzzle.tryReAuthenticate();
253+
254+
should(ret).be.false();
255+
should(kuzzle.authenticate).not.be.called();
256+
should(reconnectionErrorSpy).not.be.called();
257+
});
258+
233259
it('should call "authenticate" if the token is not valid', async () => {
234260
const ret = await kuzzle.tryReAuthenticate();
235261

test/kuzzle/protocol.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ describe('Kuzzle protocol methods', () => {
5252

5353
it('should empty the jwt when a "tokenExpired" events is triggered', () => {
5454
kuzzle.jwt = generateJwt();
55+
56+
should(kuzzle._loggedIn).be.true();
57+
5558
kuzzle.connect();
59+
kuzzle.tryReAuthenticate = sinon.stub().resolves(false);
5660

5761
kuzzle.protocol.emit('tokenExpired');
5862

59-
should(kuzzle.jwt).be.null();
63+
setTimeout(() => {
64+
should(kuzzle.tryReAuthenticate).be.calledOnce();
65+
should(kuzzle._loggedIn).be.false();
66+
should(kuzzle.jwt).be.null();
67+
}, 1);
6068
});
6169
});
6270
});

0 commit comments

Comments
 (0)