Skip to content

Commit

Permalink
fix: Allow 401 errors for expired tokens
Browse files Browse the repository at this point in the history
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 cozy/cozy-stack#3173
  • Loading branch information
nono committed Oct 25, 2021
1 parent 8d1e4bf commit a2936b5
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a2936b5

Please sign in to comment.