Skip to content

Commit

Permalink
Fetch new access and refresh tokens when refresh fails (#1338)
Browse files Browse the repository at this point in the history
* Fetch new access and refresh tokens when refresh fails

* Fix typo
  • Loading branch information
rafaelrenanpacheco authored and gschier committed Jan 21, 2019
1 parent c770946 commit 8112f5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/insomnia-app/app/network/o-auth-2/get-token.js
Expand Up @@ -190,6 +190,12 @@ async function _getAccessToken(
authentication.scope,
);

// If we didn't receive an access token it means the refresh token didn't succeed,
// so we tell caller to fetch brand new access and refresh tokens.
if (!refreshResults.access_token) {
return null;
}

// ~~~~~~~~~~~~~ //
// Update the DB //
// ~~~~~~~~~~~~~ //
Expand Down
9 changes: 8 additions & 1 deletion packages/insomnia-app/app/network/o-auth-2/refresh-token.js
Expand Up @@ -48,7 +48,14 @@ export default async function(
});

const statusCode = response.statusCode || 0;
if (statusCode < 200 || statusCode >= 300) {

if (statusCode === 401) {
// If the refresh token was rejected due an unauthorized request, we will
// return a null access_token to trigger an authentication request to fetch
// brand new refresh and access tokens.

return responseToObject(null, [c.P_ACCESS_TOKEN]);
} else if (statusCode < 200 || statusCode >= 300) {
throw new Error(`[oauth2] Failed to refresh token url=${url} status=${statusCode}`);
}

Expand Down

0 comments on commit 8112f5d

Please sign in to comment.