@@ -353,8 +353,7 @@ export class Kuzzle extends KuzzleEventEmitter {
353353 * In case of login failure we need to be sure that the stored token is still valid
354354 */
355355 try {
356- const response = await this . auth . checkToken ( ) ;
357- this . _loggedIn = response . valid ;
356+ this . _loggedIn = await this . isAuthenticated ( ) ;
358357 } catch {
359358 this . _loggedIn = false ;
360359 }
@@ -374,8 +373,7 @@ export class Kuzzle extends KuzzleEventEmitter {
374373 */
375374 this . on ( 'connected' , async ( ) => {
376375 try {
377- const { valid } = await this . auth . checkToken ( ) ;
378- this . _loggedIn = valid ;
376+ this . _loggedIn = await this . isAuthenticated ( ) ;
379377 } catch {
380378 this . _loggedIn = false ;
381379 }
@@ -654,7 +652,7 @@ export class Kuzzle extends KuzzleEventEmitter {
654652 private async tryReAuthenticate ( ) : Promise < boolean > {
655653 this . _reconnectInProgress = true ;
656654 try {
657- const { valid } = await this . auth . checkToken ( ) ;
655+ const valid = await this . isAuthenticated ( ) ;
658656
659657 if ( valid ) {
660658 return true ;
@@ -698,7 +696,7 @@ export class Kuzzle extends KuzzleEventEmitter {
698696
699697 await this . authenticator ( ) ;
700698
701- const { valid } = await this . auth . checkToken ( ) ;
699+ const valid = await this . isAuthenticated ( ) ;
702700
703701 this . _loggedIn = valid ;
704702
@@ -707,6 +705,17 @@ export class Kuzzle extends KuzzleEventEmitter {
707705 }
708706 }
709707
708+ /**
709+ * Check wether the user is authenticated or not
710+ * by verifiying if a token is present and still valid
711+ * and if the token doesn't belong to the anonymous user.
712+ */
713+ async isAuthenticated ( ) {
714+ const { valid, kuid } = await this . auth . checkToken ( ) ;
715+
716+ return valid && kuid !== '-1' ;
717+ }
718+
710719 /**
711720 * Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their
712721 * insertion.
0 commit comments