Skip to content

Commit

Permalink
Feat/ime 354 (#53)
Browse files Browse the repository at this point in the history
* feat/ime-354: Add env variable to disable authentication

* fix: yarn lint error

---------

Co-authored-by: v3-Panwar-Ujjwal <[email protected]>
  • Loading branch information
Ujjwal-Izyane and v3-Panwar-Ujjwal authored Feb 11, 2025
1 parent e0b8b42 commit fbee7fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ app.get('/config', function(req, res) {
LOGIN_URL: process.env.LOGIN_URL,
LOGIN_PROVIDER: process.env.LOGIN_PROVIDER,
LOGOUT_URL: process.env.LOGOUT_URL,
ENABLE_AUTHENTICATION: process.env.ENABLE_AUTHENTICATION,
});
});

Expand Down
56 changes: 36 additions & 20 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,45 @@ async function bootstrap() {
const uiConfigurations = await getUiConfig();
setupStyles(uiConfigurations);

// we make a blocking call to getUserInfo before rendering as this will tell
// us if we are authenticated or not. we should not render anything if we are
// not authenticated at startup.
// If we're not authenticated the call to getUserInfo will redirect the
// browser to the login URL but the browser will stupidly carry on executing
// code once we have called window.location.href=. The render block is
// wrapped in a conditional to prevent this once the redirect has been
// initiated.
const userInfo = await getUserInfo(config);
// If the authentication is not enabled don't check for the user
// authentication and render the pages
// default value is false
if (config.enableAuthentication) {
// we make a blocking call to getUserInfo before rendering as this will tell
// us if we are authenticated or not. we should not render anything if we are
// not authenticated at startup.
// If we're not authenticated the call to getUserInfo will redirect the
// browser to the login URL but the browser will stupidly carry on executing
// code once we have called window.location.href=. The render block is
// wrapped in a conditional to prevent this once the redirect has been
// initiated.
const userInfo = await getUserInfo(config);

if (userInfo) {
const user: User = {
username: userInfo.preferred_username,
givenName: userInfo.given_name,
familyName: userInfo.family_name,
email: userInfo.email,
kratos: userInfo.kratos,
logoutUrl: 'logoutUrl' in userInfo ? userInfo.logoutUrl : `${config.apiBaseUrl}/logout`,
};
if (userInfo) {
const user: User = {
username: userInfo.preferred_username,
givenName: userInfo.given_name,
familyName: userInfo.family_name,
email: userInfo.email,
kratos: userInfo.kratos,
logoutUrl: 'logoutUrl' in userInfo ? userInfo.logoutUrl : `${config.apiBaseUrl}/logout`,
};

// only render if we got user info i.e. we are authenticated
// only render if we got user info i.e. we are authenticated
store.dispatch({ type: 'App / Set Config', config });
store.dispatch({ type: 'App / Set User', data: user });
store.dispatch({ type: 'App / Set UI Config', uiConfig: uiConfigurations });
ReactDOM.render(
<React.StrictMode>
<ConnectedApp />
</React.StrictMode>,
document.getElementById('root')
);
}
} else {
// render without authentication
store.dispatch({ type: 'App / Set Config', config });
store.dispatch({ type: 'App / Set User', data: user });
store.dispatch({ type: 'App / Set User', data: null });
store.dispatch({ type: 'App / Set UI Config', uiConfig: uiConfigurations });
ReactDOM.render(
<React.StrictMode>
Expand Down
4 changes: 3 additions & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getConfig = async () => {
let loginUrl;
let loginProvider;
let logoutUrl;
let enableAuthentication = false;

try {
const { headers, data } = await axios(configURL);
Expand All @@ -21,13 +22,14 @@ export const getConfig = async () => {
loginUrl = data.LOGIN_URL;
loginProvider = data.LOGIN_PROVIDER;
logoutUrl = data.LOGOUT_URL;
enableAuthentication = data.ENABLE_AUTHENTICATION === 'true';
}
} catch (err) {
// eslint-disable-next-line
console.info('Config not found. Falling back to default values');
}

return { apiBaseUrl, checkSession, loginUrl, loginProvider, logoutUrl };
return { apiBaseUrl, checkSession, loginUrl, loginProvider, logoutUrl, enableAuthentication };
};

export const getUiConfig = async () => {
Expand Down

0 comments on commit fbee7fa

Please sign in to comment.