From 395162633ff22e95833a3c2900cb9464bb5b056f Mon Sep 17 00:00:00 2001 From: David Luecke Date: Thu, 21 Nov 2019 09:18:09 -0800 Subject: [PATCH] fix(authentication-client): Reset authentication promise on socket disconnect (#1696) --- packages/authentication-client/src/core.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/authentication-client/src/core.ts b/packages/authentication-client/src/core.ts index 59cd8fdd63..77178872f4 100644 --- a/packages/authentication-client/src/core.ts +++ b/packages/authentication-client/src/core.ts @@ -61,14 +61,18 @@ export class AuthenticationClient { handleSocket (socket: any) { // Connection events happen on every reconnect const connected = this.app.io ? 'connect' : 'open'; + const disconnected = this.app.io ? 'disconnect' : 'disconnection'; - socket.on(connected, () => { + socket.on(disconnected, () => { + const authPromise = new Promise(resolve => + socket.once(connected, () => resolve()) + ) // Only reconnect when `reAuthenticate()` or `authenticate()` // has been called explicitly first - if (this.authenticated) { - // Force reauthentication with the server - this.reAuthenticate(true); - } + // Force reauthentication with the server + .then(() => this.authenticated ? this.reAuthenticate(true) : null); + + this.app.set('authentication', authPromise); }); }