diff --git a/src/sync.js b/src/sync.js index f7f62344898..18cc579fb45 100644 --- a/src/sync.js +++ b/src/sync.js @@ -462,6 +462,16 @@ SyncApi.prototype.sync = function() { let savedSyncPromise = Promise.resolve(); let savedSyncToken = null; + async function shouldAbortSync(err) { + if (err.errcode === "M_UNKNOWN_TOKEN") { + // The logout already happened, we just need to stop. + console.warn("Token no longer valid - assuming logout"); + self.stop(); + return true; + } + return false; + } + // We need to do one-off checks before we can begin the /sync loop. // These are: // 1) We need to get push rules so we can check if events should bing as we get @@ -479,6 +489,7 @@ SyncApi.prototype.sync = function() { client.pushRules = result; } catch (err) { console.error("Getting push rules failed", err); + if (shouldAbortSync(err)) return; // wait for saved sync to complete before doing anything else, // otherwise the sync state will end up being incorrect debuglog("Waiting for saved sync before retrying push rules..."); @@ -564,6 +575,7 @@ SyncApi.prototype.sync = function() { ); } catch (err) { console.error("Getting filter failed", err); + if (shouldAbortSync(err)) return; // wait for saved sync to complete before doing anything else, // otherwise the sync state will end up being incorrect debuglog("Waiting for saved sync before retrying filter..."); @@ -877,6 +889,12 @@ SyncApi.prototype._onSyncError = function(err, syncOptions) { console.error("/sync error %s", err); console.error(err); + if (err.errcode === "M_UNKNOWN_TOKEN") { + console.warn("Access token no longer valid - stopping sync"); + this.stop(); + return; + } + this._failedSyncCount++; console.log('Number of consecutive failed sync requests:', this._failedSyncCount);