Skip to content

Commit

Permalink
remove redundant endpoints reload
Browse files Browse the repository at this point in the history
  • Loading branch information
npalaska committed Mar 9, 2023
1 parent 9995f7d commit b3a052c
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions dashboard/src/actions/authActions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from "axios";
import * as APP_ROUTES from "utils/routeConstants";
import * as CONSTANTS from "../assets/constants/authConstants";
import * as TYPES from "./types";
Expand All @@ -10,6 +11,33 @@ import { uid } from "../utils/helper";
import { fetchEndpoints } from "actions/endpointAction";


/**
* Loop to check if the endpoints are loaded.
* @param {getState} getState object.
* @return {promise} promise object
*/
function waitForEndpoints (getState) {
const waitStart = Date.now();
const maxWait = 10000; // Milliseconds
/**
* Loop to check if the endpoints are loaded.
* @param {resolve} resolve object.
* @param {reject} reject object
*/
function check(resolve, reject) {
if (Object.keys(getState().apiEndpoint.endpoints).length !== 0) {
resolve("Loaded");
} else if (Date.now() - waitStart > maxWait) {
reject(new Error('Something went wrong'));
} else {
setTimeout(check, 250, resolve, reject);
}
};
return new Promise((resolve, reject) => {
check(resolve, reject);
});
}

/**
* Get user details from the OIDC server.
* @param {endpoints} endpoints from the getState.
Expand Down Expand Up @@ -50,21 +78,20 @@ export const loadTokens = () => async (dispatch, getState) => {
}
let endpoints = getState().apiEndpoint.endpoints;
console.log(getState());
if (!("openid" in endpoints)){
await dispatch(fetchEndpoints);
}
await waitForEndpoints(getState);
endpoints = getState().apiEndpoint.endpoints;
console.log(endpoints);

const oidcServer = endpoints.openid.server
const oidcRealm = endpoints.openid.realm
const oidcClient = endpoints.openid.client
const uri = `${oidcServer}/realms/${oidcRealm}/protocol/openid-connect/token`;
const queryParams = [
'grant_type=authorization_code',
'client_id=' + oidcClient,
'code=' + code,
'redirect_uri=' + window.location.href.split('?')[0],
];
const queryParams = new URLSearchParams({
grant_type: "authorization_code",
client_id: oidcClient,
code: code,
redirect_uri: window.location.href.split('?')[0]
});

const req = new XMLHttpRequest();
req.onreadystatechange = async function () {
Expand Down Expand Up @@ -104,7 +131,7 @@ export const loadTokens = () => async (dispatch, getState) => {
}
req.open('POST', uri);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send(queryParams.join('&'));
req.send(queryParams);
}

// Create an Authentication Request
Expand Down

0 comments on commit b3a052c

Please sign in to comment.