Skip to content

Commit

Permalink
Stop syncing when the token is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Apr 15, 2019
1 parent 34309da commit dcd9b5c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...");
Expand Down Expand Up @@ -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...");
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit dcd9b5c

Please sign in to comment.