Skip to content

Commit

Permalink
address partial comments
Browse files Browse the repository at this point in the history
  • Loading branch information
npalaska committed Mar 2, 2023
1 parent f1d8670 commit 9995f7d
Showing 1 changed file with 57 additions and 76 deletions.
133 changes: 57 additions & 76 deletions dashboard/src/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ import { uid } from "../utils/helper";
import { fetchEndpoints } from "actions/endpointAction";


/**
* Get the key value.
* @param {key} key can be a string.
* @return {value} a string value of a key
*/
export function getQueryVariable (key) {
const query = window.location.search.substring(1);
const vars = query.split('&');
let code = "";
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
if (pair[0] === key) {
code = pair[1];
}
// else return "no-idea";
}
return code;

}

/**
* Get user details from the OIDC server.
* @param {endpoints} endpoints from the getState.
Expand Down Expand Up @@ -63,67 +43,68 @@ export function getOIDCUserDetails(endpoints) {
}

export const loadTokens = () => async (dispatch, getState) => {
const code = getQueryVariable('code');
const code = new URLSearchParams(window.location.search).get("code");

if (code !== "") {
let endpoints = getState().apiEndpoint.endpoints;
if (!("openid" in endpoints)){
await dispatch(fetchEndpoints);
}
endpoints = getState().apiEndpoint.endpoints;
if (code === null) {
return;
}
let endpoints = getState().apiEndpoint.endpoints;
console.log(getState());
if (!("openid" in endpoints)){
await dispatch(fetchEndpoints);
}
endpoints = getState().apiEndpoint.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 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 req = new XMLHttpRequest();
req.onreadystatechange = async function () {
if (req.readyState === 4) {
const response = JSON.parse(req.responseText);
const keepUser = getState().userAuth.keepLoggedIn;
const expiryTime = keepUser
? CONSTANTS.EXPIRY_KEEPUSER_DAYS
: CONSTANTS.EXPIRY_DEFAULT_DAYS;
Cookies.set("isLoggedIn", true, { expires: expiryTime });
Cookies.set("token", response["access_token"], {
expires: response["expires_in"],
});
let userPayload = {};
await getOIDCUserDetails(endpoints).then(res => userPayload = res);
dispatch({
type: TYPES.GET_USER_DETAILS,
payload: userPayload,
});
Cookies.set("refresh_token", response["refresh_token"], {
expires: response["refresh_expires_in"],
});
Cookies.set("username", userPayload.username, {
expires: expiryTime,
});
const loginDetails = {
isLoggedIn: true,
token: response["access_token"],
username: userPayload.username,
};
dispatch({
type: TYPES.SET_LOGIN_DETAILS,
payload: loginDetails,
});
dispatch(showToast(SUCCESS, "Logged in successfully!"));
}
dispatch({ type: TYPES.COMPLETED });
const req = new XMLHttpRequest();
req.onreadystatechange = async function () {
if (req.readyState === 4) {
const response = JSON.parse(req.responseText);
const keepUser = getState().userAuth.keepLoggedIn;
const expiryTime = keepUser
? CONSTANTS.EXPIRY_KEEPUSER_DAYS
: CONSTANTS.EXPIRY_DEFAULT_DAYS;
Cookies.set("isLoggedIn", true, { expires: expiryTime });
Cookies.set("token", response["access_token"], {
expires: response["expires_in"],
});
const userPayload = await getOIDCUserDetails(endpoints);
dispatch({
type: TYPES.GET_USER_DETAILS,
payload: userPayload,
});
Cookies.set("refresh_token", response["refresh_token"], {
expires: response["refresh_expires_in"],
});
Cookies.set("username", userPayload.username, {
expires: expiryTime,
});
const loginDetails = {
isLoggedIn: true,
token: response["access_token"],
username: userPayload.username,
};
dispatch({
type: TYPES.SET_LOGIN_DETAILS,
payload: loginDetails,
});
dispatch(showToast(SUCCESS, "Logged in successfully!"));
}
req.open('POST', uri);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send(queryParams.join('&'));
dispatch({ type: TYPES.COMPLETED });
}
req.open('POST', uri);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send(queryParams.join('&'));
}

// Create an Authentication Request
Expand Down

0 comments on commit 9995f7d

Please sign in to comment.