From 47f9bdb6c1b7237a64ce2a9e0789f1f00da618b3 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Wed, 20 Nov 2019 13:29:24 -0800 Subject: [PATCH] fix(authentication-client): Reset authentication promise on socket disconnect --- 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); }); }