Skip to content

Commit 1f04b94

Browse files
author
Shiranuit
authored
Merge pull request #656 from kuzzleio/7.7.3-proposal
Release 7.7.3
2 parents 8c7b354 + 98ef492 commit 1f04b94

File tree

14 files changed

+5431
-4727
lines changed

14 files changed

+5431
-4727
lines changed

doc/7/getting-started/.react-native/package-lock.json

Lines changed: 767 additions & 379 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/7/getting-started/.react-native/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"test": "node_modules/.bin/cypress run --record --key $CYPRESS_RECORD_KEY_DOC"
1111
},
1212
"dependencies": {
13-
"cypress": "^3.8.1",
13+
"cypress": "^8.1.0",
1414
"expo": "^35.0.0",
1515
"expo-cli": "^3.11.5",
1616
"kuzzle-sdk": "^7.0.0",

doc/7/getting-started/.vuejs/package-lock.json

Lines changed: 4366 additions & 4278 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/7/getting-started/.vuejs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"serve-with-vuex": "cd with-vuex && vue-cli-service serve",
1111
"build-with-vuex": "cd with-vuex && vue-cli-service build",
1212
"lint-with-vuex": "cd with-vuex && vue-cli-service lint",
13-
"test": "./node_modules/.bin/cypress run --record --key $CYPRESS_RECORD_KEY_DOC"
13+
"test": "./node_modules/.bin/cypress run"
1414
},
1515
"dependencies": {
1616
"core-js": "^2.6.5",
@@ -23,7 +23,7 @@
2323
"@vue/cli-plugin-eslint": "^3.7.0",
2424
"@vue/cli-service": "^3.7.0",
2525
"babel-eslint": "^10.0.1",
26-
"cypress": "^3.3.1",
26+
"cypress": "^8.1.0",
2727
"eslint": "^5.16.0",
2828
"eslint-plugin-vue": "^5.0.0",
2929
"vue-template-compiler": "^2.5.21"

package-lock.json

Lines changed: 3 additions & 3 deletions
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.2",
3+
"version": "7.7.3",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/Kuzzle.ts

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ const events = [
2727
'discarded',
2828
'disconnected',
2929
'loginAttempt',
30+
'logoutAttempt',
3031
'networkError',
3132
'offlineQueuePush',
3233
'offlineQueuePop',
3334
'queryError',
35+
'reAuthenticated',
3436
'reconnected',
3537
'reconnectionError',
3638
'tokenExpired'
@@ -98,6 +100,8 @@ export class Kuzzle extends KuzzleEventEmitter {
98100
private _tokenExpiredInterval: any;
99101
private _lastTokenExpired: any;
100102
private _cookieAuthentication: boolean;
103+
private _reconnectInProgress: boolean;
104+
private _loggedIn: boolean;
101105

102106
private __proxy__: any;
103107

@@ -323,6 +327,48 @@ export class Kuzzle extends KuzzleEventEmitter {
323327

324328
this._lastTokenExpired = null;
325329

330+
this._reconnectInProgress = false;
331+
332+
this._loggedIn = false;
333+
334+
this.on('loginAttempt', async status => {
335+
if (status.success) {
336+
this._loggedIn = true;
337+
return;
338+
}
339+
340+
/**
341+
* In case of login failure we need to be sure that the stored token is still valid
342+
*/
343+
try {
344+
const response = await this.auth.checkToken();
345+
this._loggedIn = response.valid;
346+
} catch {
347+
this._loggedIn = false;
348+
}
349+
});
350+
351+
/**
352+
* When successfuly logged out
353+
*/
354+
this.on('logoutAttempt', status => {
355+
if (status.success) {
356+
this._loggedIn = false;
357+
}
358+
});
359+
360+
/**
361+
* On connection we need to verify if the token is still valid to know if we are still "logged in"
362+
*/
363+
this.on('connected', async () => {
364+
try {
365+
const { valid } = await this.auth.checkToken();
366+
this._loggedIn = valid;
367+
} catch {
368+
this._loggedIn = false;
369+
}
370+
});
371+
326372
return proxify(this, {
327373
seal: true,
328374
name: 'kuzzle',
@@ -531,31 +577,44 @@ export class Kuzzle extends KuzzleEventEmitter {
531577
this.emit('disconnected', context);
532578
});
533579

534-
this.protocol.addListener('reconnect', async () => {
535-
if (this.autoQueue) {
536-
this.stopQueuing();
537-
}
538-
539-
// If an authenticator was set, check if the token is still valid and try
540-
// to re-authenticate if needed. Otherwise the SDK is in disconnected state.
541-
if (this.authenticator && ! await this.tryReAuthenticate()) {
542-
this.disconnect();
543-
544-
return;
545-
}
546-
547-
if (this.autoReplay) {
548-
this.playQueue();
549-
}
550-
551-
this.emit('reconnected');
552-
});
580+
this.protocol.addListener('reconnect', this._reconnect.bind(this));
553581

554582
this.protocol.addListener('discarded', data => this.emit('discarded', data));
555583

584+
this.protocol.addListener('websocketRenewalStart', () => { this._reconnectInProgress = true; });
585+
this.protocol.addListener('websocketRenewalDone', () => { this._reconnectInProgress = false; });
586+
556587
return this.protocol.connect();
557588
}
558589

590+
async _reconnect() {
591+
if (this._reconnectInProgress) {
592+
return;
593+
}
594+
595+
if (this.autoQueue) {
596+
this.stopQueuing();
597+
}
598+
599+
// If an authenticator was set, check if a user was logged in and if the token is still valid and try
600+
// to re-authenticate if needed. Otherwise the SDK is in disconnected state.
601+
if ( this._loggedIn
602+
&& this.authenticator
603+
&& ! await this.tryReAuthenticate()
604+
) {
605+
this._loggedIn = false;
606+
this.disconnect();
607+
608+
return;
609+
}
610+
611+
if (this.autoReplay) {
612+
this.playQueue();
613+
}
614+
615+
this.emit('reconnected');
616+
}
617+
559618
/**
560619
* Try to re-authenticate the SDK if the current token is invalid.
561620
*
@@ -567,6 +626,7 @@ export class Kuzzle extends KuzzleEventEmitter {
567626
* This method never returns a rejected promise.
568627
*/
569628
private async tryReAuthenticate (): Promise<boolean> {
629+
this._reconnectInProgress = true;
570630
try {
571631
const { valid } = await this.auth.checkToken();
572632

@@ -584,6 +644,8 @@ export class Kuzzle extends KuzzleEventEmitter {
584644
});
585645

586646
return false;
647+
} finally {
648+
this._reconnectInProgress = false;
587649
}
588650
}
589651

@@ -601,6 +663,8 @@ export class Kuzzle extends KuzzleEventEmitter {
601663

602664
const { valid } = await this.auth.checkToken();
603665

666+
this._loggedIn = valid;
667+
604668
if (! valid) {
605669
throw new Error('The "authenticator" function failed to authenticate the SDK.');
606670
}
@@ -639,6 +703,7 @@ export class Kuzzle extends KuzzleEventEmitter {
639703
* Disconnects from Kuzzle and invalidate this instance.
640704
*/
641705
disconnect () {
706+
this._loggedIn = false;
642707
this.protocol.close();
643708
}
644709

@@ -769,7 +834,17 @@ Discarded request: ${JSON.stringify(request)}`));
769834
* On token expiration, reset jwt and unsubscribe all rooms.
770835
* Throttles to avoid duplicate event triggers.
771836
*/
772-
tokenExpired() {
837+
async tokenExpired () {
838+
if (this._reconnectInProgress) {
839+
return;
840+
}
841+
842+
if (this._loggedIn && this.authenticator && await this.tryReAuthenticate()) {
843+
this.emit('reAuthenticated');
844+
845+
return;
846+
}
847+
773848
const now = Date.now();
774849

775850
if ((now - this._lastTokenExpired) < this.tokenExpiredInterval) {

src/controllers/Auth.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ export class AuthController extends BaseController {
468468
.then(response => {
469469
if (this.kuzzle.cookieAuthentication) {
470470
if (response.result.jwt) {
471-
throw new Error('Kuzzle support for cookie authentication is disabled or not supported');
471+
const err = new Error('Kuzzle support for cookie authentication is disabled or not supported');
472+
this.kuzzle.emit('loginAttempt', { success: false, error: err.message });
473+
throw err;
472474
}
473475

474476
this.kuzzle.emit('loginAttempt', { success: true });
@@ -499,6 +501,7 @@ export class AuthController extends BaseController {
499501
}, { queuable: false, timeout: -1 })
500502
.then(() => {
501503
this._authenticationToken = null;
504+
this.kuzzle.emit('logoutAttempt', { success: true });
502505
});
503506
}
504507

src/controllers/Realtime.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export class RealtimeController extends BaseController {
6060
this.kuzzle.on('disconnected', () => this.saveSubscriptions());
6161
this.kuzzle.on('networkError', () => this.saveSubscriptions());
6262
this.kuzzle.on('reconnected', () => this.resubscribe());
63+
this.kuzzle.on('reAuthenticated', () => {
64+
this.saveSubscriptions();
65+
this.resubscribe();
66+
});
6367
}
6468

6569
/**

0 commit comments

Comments
 (0)