From a2936b5ae0bce2a5bec561e357aa55958e02b0b4 Mon Sep 17 00:00:00 2001 From: Bruno Michel Date: Mon, 4 Oct 2021 16:53:39 +0200 Subject: [PATCH] fix: Allow 401 errors for expired tokens The stack will change the http code used for expired tokens from 400 to 401. This change will allow cozy-client-js to try refreshing the token in such cases. See https://github.com/cozy/cozy-stack/pull/3173 --- src/fetch.js | 2 +- src/offline.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fetch.js b/src/fetch.js index 4e6b6e07..2a93c903 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -184,7 +184,7 @@ FetchError.isInvalidToken = function(err) { // XXX We can't use err instanceof FetchError because of the caveats of babel return ( err.name === 'FetchError' && - err.status === 400 && + (err.status === 400 || err.status === 401) && err.reason && (err.reason.error === 'Invalid JWT token' || err.reason.error === 'Expired token') diff --git a/src/offline.js b/src/offline.js index d421c031..35c56ac1 100644 --- a/src/offline.js +++ b/src/offline.js @@ -181,7 +181,7 @@ export function replicateFromCozy(cozy, doctype, options = {}) { options.onComplete && options.onComplete(info) }) .on('error', err => { - if (err.error === 'code=400, message=Expired token') { + if (/Expired token/.test(err.error)) { cozy.authorize().then(({ client, token }) => { refreshToken(cozy, client, token) .then(newToken => cozy.saveCredentials(client, newToken))