Skip to content

Commit

Permalink
fix: avoid redirect loops when axios calls an unauthorized API (#6450)
Browse files Browse the repository at this point in the history
* fix: avoid redirect loops when axios calls an unauthorized API

* use the proper structure for axios

* protect against empty request data
  • Loading branch information
elevatebart authored Dec 13, 2024
1 parent aae4e6b commit ed264a5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ui/src/utils/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ let requestsTotal = 0
let requestsCompleted = 0
let latencyThreshold = 0

const JWT_REFRESHED_QUERY = "__jwt_refreshed__";

const progressComplete = () => {
requestsTotal = 0
requestsCompleted = 0
Expand Down Expand Up @@ -115,6 +117,14 @@ export default (callback, store, router) => {
const originalRequest = errorResponse.config

if (!refreshing) {
const originalRequestData = JSON.parse(originalRequest.data ?? "{}");

// if we already tried refreshing the token,
// the user simply does not have access to this feature
if(originalRequestData[JWT_REFRESHED_QUERY] === 1) {
return Promise.reject(errorResponse)
}

refreshing = true;
try {
await instance.post("/oauth/access_token?grant_type=refresh_token", null, {headers: {"Content-Type": "application/json"}});
Expand All @@ -124,8 +134,9 @@ export default (callback, store, router) => {
toRefreshQueue = [];
refreshing = false;

originalRequestData[JWT_REFRESHED_QUERY] = 1;
originalRequest.data = JSON.stringify(originalRequestData);
return instance(originalRequest)

} catch {
document.body.classList.add("login");
store.dispatch("core/isUnsaved", false);
Expand Down

0 comments on commit ed264a5

Please sign in to comment.