Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid redirect loops when axios calls an unauthorized API #6450

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading