From db7787c38142bef830b3e1540bff39b0783e2168 Mon Sep 17 00:00:00 2001 From: Simon Williams Date: Mon, 10 May 2021 23:22:51 +0100 Subject: [PATCH] Automatically re-auth in case the token is invalid after reconnecting --- src/Socket.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Socket.js b/src/Socket.js index 2dda0f5..fff0548 100644 --- a/src/Socket.js +++ b/src/Socket.js @@ -61,7 +61,7 @@ export class Socket extends EventEmitter { } debug('connected') this.emit('connected') - resolve(this.auth(this.api.token)) + resolve(this._auth()) }) this.ws.on('close', () => { clearInterval(this.keepAliveInter) @@ -172,6 +172,21 @@ export class Socket extends EventEmitter { } } + async _auth () { + try { + await this.auth(this.api.token) + } catch (err) { + if (err.message === 'socket auth failed') { + // Retry with a new token (in case the token is invalid after a reconnect) + debug('auth retry') + await this.api.auth() + await this.auth(this.api.token) + } else { + throw err + } + } + } + auth (token) { return new Promise((resolve, reject) => { this.send(`auth ${token}`)