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

Get OIDC access tokens once the authentication redirect is successful #3250

Merged
24 changes: 12 additions & 12 deletions dashboard/src/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import * as TYPES from "./types";

import Cookies from "js-cookie";
import { SUCCESS } from "assets/constants/overviewConstants";
import { showToast } from "actions/toastActions";
import { showToast, clearToast } from "actions/toastActions";

const maxWait = 50000; // Milliseconds
/**
* Wait for the Pbench Server endpoints to be loaded.
* @param {getState} getState object.
* @return {promise} promise object
*/
export function waitForEndpoints(getState) {
export async function waitForEndpoints(getState) {
webbnh marked this conversation as resolved.
Show resolved Hide resolved
const waitStart = Date.now();
/**
* Settle the wait-for-endpoints promise.
Expand All @@ -22,7 +21,7 @@ export function waitForEndpoints(getState) {
function check(resolve, reject) {
if (Object.keys(getState().apiEndpoint.endpoints).length !== 0) {
resolve("Endpoints loaded");
} else if (Date.now() - waitStart > maxWait) {
} else if (Date.now() - waitStart > CONSTANTS.MAX_WAIT_MS) {
webbnh marked this conversation as resolved.
Show resolved Hide resolved
reject(new Error("Timed out waiting for endpoints request"));
} else {
setTimeout(check, 250, resolve, reject);
Expand All @@ -47,25 +46,26 @@ export const authCookies = () => async (dispatch, getState) => {
};

export const movePage = (toPage, navigate) => async (dispatch) => {
// empty the alerts
dispatch({
type: TYPES.USER_NOTION_ALERTS,
payload: [],
});
// clear all the toasts before navigating to another page
dispatch(clearToast());
navigate(toPage);
};

export const clearCachedSession = () => async (dispatch, getState) => {
/**
* Clear the local cookies and re-direct to the auth page.
* @param {dispatch} dispatch object.
*/
export async function clearCachedSession(dispatch) {
dispatch({ type: TYPES.LOADING });
Cookies.remove("isLoggedIn");
dispatch({ type: TYPES.COMPLETED });
setTimeout(() => {
window.location.href = APP_ROUTES.AUTH;
}, CONSTANTS.LOGOUT_DELAY_MS);
};
}
webbnh marked this conversation as resolved.
Show resolved Hide resolved

export const sessionLogout = () => async (dispatch, getState) => {
const keycloak = getState().apiEndpoint.keycloak;
keycloak.logout();
dispatch(clearCachedSession());
clearCachedSession(dispatch);
};
13 changes: 10 additions & 3 deletions dashboard/src/actions/overviewActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import API from "../utils/axiosInstance";
import { DANGER } from "assets/constants/toastConstants";
import { findNoOfDays } from "utils/dateFunctions";
import { showToast } from "./toastActions";
import { clearCachedSession } from "./authActions";

export const getDatasets = () => async (dispatch, getState) => {
const alreadyRendered = getState().overview.loadingDone;
try {
const keycloak = getState().apiEndpoint.keycloak;
const username = keycloak?.idTokenParsed?.preferred_username;
const username = keycloak.idTokenParsed.preferred_username;

if (alreadyRendered) {
dispatch({ type: TYPES.LOADING });
Expand Down Expand Up @@ -43,8 +44,14 @@ export const getDatasets = () => async (dispatch, getState) => {
}
}
} catch (error) {
dispatch(showToast(DANGER, error?.response?.data?.message));
dispatch({ type: TYPES.NETWORK_ERROR });
if (!error?.response) {
dispatch(showToast(DANGER, "Not Authenticated"));
dispatch({ type: TYPES.OPENID_ERROR });
clearCachedSession(dispatch);
} else {
dispatch(showToast(DANGER, error?.response?.data?.message));
webbnh marked this conversation as resolved.
Show resolved Hide resolved
dispatch({ type: TYPES.NETWORK_ERROR });
}
}
if (alreadyRendered) {
dispatch({ type: TYPES.COMPLETED });
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/actions/toastActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const showSessionExpired = () => async (dispatch) => {
message: "Please login to continue",
};
dispatch(showToast(toast.variant, toast.title, toast.message));
dispatch(clearCachedSession());
clearCachedSession(dispatch);
};

export const showFailureToast = () => async (dispatch) => {
Expand Down
3 changes: 1 addition & 2 deletions dashboard/src/assets/constants/authConstants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const LOGOUT_DELAY_MS = 2000;
export const EXPIRY_KEEPUSER_DAYS = 7;
export const EXPIRY_DEFAULT_DAYS = 0.5;
export const MAX_WAIT_MS = 10000;
webbnh marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 12 additions & 4 deletions dashboard/src/modules/components/ProfileComponent/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ const ProfileComponent = () => {
{
<TextContent>
<Text component={TextVariants.h5}>
{keycloak.tokenParsed?.given_name}
{keycloak.tokenParsed?.given_name
? keycloak.tokenParsed?.given_name
: ""}
webbnh marked this conversation as resolved.
Show resolved Hide resolved
</Text>
</TextContent>
}
Expand All @@ -64,7 +66,9 @@ const ProfileComponent = () => {
{
<TextContent>
<Text component={TextVariants.h5}>
{keycloak.tokenParsed?.family_name}
{keycloak.tokenParsed?.family_name
? keycloak.tokenParsed?.family_name
: ""}
</Text>
</TextContent>
}
Expand All @@ -76,7 +80,9 @@ const ProfileComponent = () => {
{
<TextContent>
<Text component={TextVariants.h5}>
{keycloak.tokenParsed?.preferred_username}
{keycloak.tokenParsed?.preferred_username
? keycloak.tokenParsed?.preferred_username
: ""}
</Text>
</TextContent>
}
Expand All @@ -86,7 +92,9 @@ const ProfileComponent = () => {
{
<TextContent>
<Text component={TextVariants.h5}>
{keycloak.tokenParsed?.email}
{keycloak.tokenParsed?.email
? keycloak.tokenParsed?.email
: ""}
</Text>
</TextContent>
}
Expand Down
20 changes: 0 additions & 20 deletions dashboard/src/reducers/authReducer.js
webbnh marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

2 changes: 0 additions & 2 deletions dashboard/src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AuthReducer from "./authReducer";
import DatasetListReducer from "./datasetListReducer";
import EndpointReducer from "./endpointReducer";
import LoadingReducer from "./loadingReducer";
Expand All @@ -12,7 +11,6 @@ import { combineReducers } from "redux";
export default combineReducers({
toastReducer: ToastReducer,
loading: LoadingReducer,
userAuth: AuthReducer,
navOpen: NavbarReducer,
datasetlist: DatasetListReducer,
apiEndpoint: EndpointReducer,
Expand Down