Skip to content

Commit

Permalink
Remove old authentication flag and update fn call
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Jun 14, 2024
1 parent b98f229 commit aa48c98
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@gridsuite/commons-ui": "0.53.0",
"@gridsuite/commons-ui": "0.60.0",
"@hookform/resolvers": "^3.3.4",
"@mui/icons-material": "^5.15.14",
"@mui/lab": "^5.0.0-alpha.169",
Expand Down
81 changes: 41 additions & 40 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import { AppState } from '../redux/reducer';
import {
ConfigParameters,
connectNotificationsWsUpdateConfig,
fetchAuthorizationCodeFlowFeatureFlag,
fetchConfigParameter,
fetchConfigParameters,
fetchIdpSettings,
fetchValidateUser,
} from '../utils/rest-api';
import {
Expand Down Expand Up @@ -139,50 +139,45 @@ const App: FunctionComponent = () => {
})
);

const initialize = useCallback((): Promise<unknown | undefined> => {
if (process.env.REACT_APP_USE_AUTHENTICATION === 'true') {
return fetchAuthorizationCodeFlowFeatureFlag().then(
(authorizationCodeFlowEnabled) =>
initializeAuthenticationProd(
dispatch,
initialMatchSilentRenewCallbackUrl != null,
fetch('idpSettings.json'),
fetchValidateUser,
authorizationCodeFlowEnabled,
initialMatchSigninCallbackUrl != null
)
);
} else {
return initializeAuthenticationDev(
dispatch,
initialMatchSilentRenewCallbackUrl != null,
() =>
new Promise((resolve) =>
window.setTimeout(() => resolve(true), 500)
),
initialMatchSigninCallbackUrl != null
);
}
// Note: initialMatchSilentRenewCallbackUrl and dispatch don't change
}, [
initialMatchSilentRenewCallbackUrl,
dispatch,
initialMatchSigninCallbackUrl,
]);

useEffect(() => {
initialize()
.then((userManager) => {
setUserManager({ instance: userManager ?? null, error: null });
})
.catch((error: unknown) => {
// need subfunction when async as suggested by rule react-hooks/exhaustive-deps
(async function initializeAuthentication() {
try {
console.debug(
`auth dev mode: ${process.env.REACT_APP_USE_AUTHENTICATION}`
);
setUserManager({
instance:
(await (process.env.REACT_APP_USE_AUTHENTICATION ===
'true'
? initializeAuthenticationProd(
dispatch,
initialMatchSilentRenewCallbackUrl != null,
fetchIdpSettings,
fetchValidateUser,
initialMatchSigninCallbackUrl != null
)
: initializeAuthenticationDev(
dispatch,
initialMatchSilentRenewCallbackUrl != null,
validateUserDev,
initialMatchSigninCallbackUrl != null
))) ?? null,
error: null,
});
} catch (error) {
setUserManager({
instance: null,
error: getErrorMessage(error),
});
});
// Note: initialize and initialMatchSilentRenewCallbackUrl won't change
}, [initialize, initialMatchSilentRenewCallbackUrl, dispatch]);
}
})();
// Note: dispatch and initialMatchSilentRenewCallbackUrl won't change
}, [
initialMatchSigninCallbackUrl,
initialMatchSilentRenewCallbackUrl,
dispatch,
]);

useEffect(() => {
if (user !== null) {
Expand Down Expand Up @@ -280,3 +275,9 @@ const App: FunctionComponent = () => {
);
};
export default App;

function validateUserDev() {
return new Promise((resolve) =>
window.setTimeout(() => resolve(true), 500)
);
}
26 changes: 2 additions & 24 deletions src/utils/rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,8 @@ function fetchEnv(): Promise<EnvJson> {
return fetch('env.json').then((res: Response) => res.json());
}

export function fetchAuthorizationCodeFlowFeatureFlag(): Promise<boolean> {
console.info(`Fetching authorization code flow feature flag...`);
return fetchEnv()
.then((env: EnvJson) =>
fetch(`${env.appsMetadataServerUrl}/authentication.json`)
)
.then((res: Response) => res.json())
.then((res: { authorizationCodeFlowFeatureFlag: boolean }) => {
console.log(
`Authorization code flow is ${
res.authorizationCodeFlowFeatureFlag
? 'enabled'
: 'disabled'
}`
);
return res.authorizationCodeFlowFeatureFlag || false;
})
.catch((error) => {
console.error(error);
console.warn(
`Something wrong happened when retrieving authentication.json: authorization code flow will be disabled`
);
return false;
});
export function fetchIdpSettings() {
return fetch('idpSettings.json').then((res) => res.json());
}

export type VersionJson = {
Expand Down

0 comments on commit aa48c98

Please sign in to comment.