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

rework : split AuthenticationRouter into atomic component #542

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 14 additions & 109 deletions src/components/AuthenticationRouter/AuthenticationRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,19 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { Dispatch, useCallback } from 'react';
import {
Location,
Navigate,
NavigateFunction,
Route,
Routes,
} from 'react-router-dom';
import { Alert, AlertTitle, Grid } from '@mui/material';
import { FormattedMessage } from 'react-intl';
import { UserManager } from 'oidc-client';
import { useCallback } from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';
import { Grid } from '@mui/material';
import SignInCallbackHandler from '../SignInCallbackHandler';
import {
handleSigninCallback,
handleSilentRenewCallback,
login,
logout,
} from '../../utils/AuthService';
import SilentRenewCallbackHandler from '../SilentRenewCallbackHandler';
import Login from '../Login';
import Logout from '../Login/Logout';

import { AuthenticationActions } from '../../redux/authActions';

export type AuthenticationRouterErrorState = {
userName?: string;
userValidationError?: { error: Error };
logoutError?: { error: Error };
unauthorizedUserInfo?: string;
};

export type UserManagerState = {
instance: UserManager | null;
error: string | null;
};

export interface AuthenticationRouterProps {
userManager: UserManagerState;
signInCallbackError: Error | null;
authenticationRouterError: AuthenticationRouterErrorState | null;
showAuthenticationRouterLogin: boolean;
dispatch: Dispatch<AuthenticationActions>;
navigate: NavigateFunction;
location: Location;
}
import { AuthenticationRouterProps } from './authenticationType';
import AuthenticationRouterErrorDisplay from './AuthenticationRouterErrorDisplay';

function AuthenticationRouter({
userManager,
Expand All @@ -61,12 +29,12 @@ function AuthenticationRouter({
location,
}: Readonly<AuthenticationRouterProps>) {
const handleSigninCallbackClosure = useCallback(() => {
if (userManager.instance != null) {
if (userManager.instance) {
handleSigninCallback(dispatch, navigate, userManager.instance);
}
}, [dispatch, navigate, userManager.instance]);
const handleSilentRenewCallbackClosure = useCallback(() => {
if (userManager.instance != null) {
if (userManager.instance) {
handleSilentRenewCallback(userManager.instance);
}
}, [userManager.instance]);
Expand All @@ -77,10 +45,10 @@ function AuthenticationRouter({
alignItems="center"
direction="column"
>
{userManager.error !== null && (
{userManager.error && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have an empty string if we haven't retrieve the error message.

<h1>Error : Getting userManager; {userManager.error}</h1>
)}
{signInCallbackError !== null && (
{signInCallbackError && (
<h1>
Error : SignIn Callback Error;
{signInCallbackError.message}
Expand Down Expand Up @@ -125,74 +93,11 @@ function AuthenticationRouter({
</Routes>

{authenticationRouterError !== null && (
<>
<Grid item>
<Logout
disabled={userManager.instance === null}
onLogoutClick={() =>
logout(dispatch, userManager.instance)
}
/>
</Grid>
<Grid item xs={4}>
{authenticationRouterError.logoutError != null && (
<Alert severity="error">
<AlertTitle>
<FormattedMessage id="login/errorInLogout" />
</AlertTitle>
<FormattedMessage
id="login/errorInLogoutMessage"
values={{
userName:
authenticationRouterError.userName,
}}
/>
<p>
{
authenticationRouterError.logoutError
.error.message
}
</p>
</Alert>
)}
{authenticationRouterError?.userValidationError !=
null && (
<Alert severity="error">
<AlertTitle>
<FormattedMessage id="login/errorInUserValidation" />
</AlertTitle>
<FormattedMessage
id="login/errorInUserValidationMessage"
values={{
userName:
authenticationRouterError.userName,
}}
/>
<p>
{
authenticationRouterError
.userValidationError.error.message
}
</p>
</Alert>
)}
{authenticationRouterError?.unauthorizedUserInfo !=
null && (
<Alert severity="info">
<AlertTitle>
<FormattedMessage id="login/unauthorizedAccess" />
</AlertTitle>
<FormattedMessage
id="login/unauthorizedAccessMessage"
values={{
userName:
authenticationRouterError.userName,
}}
/>
</Alert>
)}
</Grid>
</>
<AuthenticationRouterErrorDisplay
dispatch={dispatch}
instance={userManager.instance}
errorState={authenticationRouterError}
/>
)}
</Grid>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { Grid } from '@mui/material';
import Logout from '../Login/Logout';
import ErrorInLogoutAlert from './alert/ErrorInLogoutAlert';
import ErrorInUserValidationAlert from './alert/ErrorInUserValidationAlert';
import UnauthorizedAccessAlert from './alert/UnauthorizedAccessAlert';
import {
AuthenticationRouterErrorState,
AuthenticationRouterProps,
UserManagerState,
} from './authenticationType';
import { logout } from '../../utils/AuthService';

type AuthenticationRouterErrorDisplayProps = {
errorState: AuthenticationRouterErrorState;
instance: UserManagerState['instance'];
dispatch: AuthenticationRouterProps['dispatch'];
};

function AuthenticationRouterErrorDisplay({
errorState,
instance,
dispatch,
}: AuthenticationRouterErrorDisplayProps) {
return (
<>
<Grid item>
<Logout
disabled={instance === null}
onLogoutClick={() => logout(dispatch, instance)}
/>
</Grid>
<Grid item xs={4}>
{errorState.logoutError && (
<ErrorInLogoutAlert
userName={errorState.userName}
message={errorState.logoutError.error.message}
/>
)}
{errorState.userValidationError && (
<ErrorInUserValidationAlert
userName={errorState.userName}
message={errorState.userValidationError.error.message}
/>
)}
{errorState.unauthorizedUserInfo && (
<UnauthorizedAccessAlert userName={errorState.userName} />
)}
</Grid>
</>
);
}
export default AuthenticationRouterErrorDisplay;
30 changes: 30 additions & 0 deletions src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { Alert, AlertTitle } from '@mui/material';
import { FormattedMessage } from 'react-intl';

type ErrorInLogoutAlertProps = {
userName?: string;
message: string;
};
function ErrorInLogoutAlert({ userName, message }: ErrorInLogoutAlertProps) {
return (
<Alert severity="error">
<AlertTitle>
<FormattedMessage id="login/errorInLogout" />
</AlertTitle>
<FormattedMessage
id="login/errorInLogoutMessage"
values={{ userName }}
/>
<p>{message}</p>
</Alert>
);
}

export default ErrorInLogoutAlert;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { Alert, AlertTitle } from '@mui/material';
import { FormattedMessage } from 'react-intl';

type ErrorInUserValidationAlertProps = {
userName?: string;
message: string;
};
function ErrorInUserValidationAlert({
userName,
message,
}: ErrorInUserValidationAlertProps) {
return (
<Alert severity="error">
<AlertTitle>
<FormattedMessage id="login/errorInUserValidation" />
</AlertTitle>
<FormattedMessage
id="login/errorInUserValidationMessage"
values={{ userName }}
/>
<p>{message}</p>
</Alert>
);
}
export default ErrorInUserValidationAlert;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { Alert, AlertTitle } from '@mui/material';
import { FormattedMessage } from 'react-intl';

type UnauthorizedAccessAlertProps = { userName?: string };
function UnauthorizedAccessAlert({ userName }: UnauthorizedAccessAlertProps) {
return (
<Alert severity="info">
<AlertTitle>
<FormattedMessage id="login/unauthorizedAccess" />
</AlertTitle>
<FormattedMessage
id="login/unauthorizedAccessMessage"
values={{ userName }}
/>
</Alert>
);
}
export default UnauthorizedAccessAlert;
33 changes: 33 additions & 0 deletions src/components/AuthenticationRouter/authenticationType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { UserManager } from 'oidc-client';
import { Dispatch } from 'react';
import { NavigateFunction, Location } from 'react-router-dom';
import { AuthenticationActions } from '../../redux/authActions';

export type AuthenticationRouterErrorState = {
userName?: string;
userValidationError?: { error: Error };
logoutError?: { error: Error };
unauthorizedUserInfo?: string;
};

export type UserManagerState = {
instance: UserManager | null;
error: string | null;
};

export interface AuthenticationRouterProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we keep the props near the component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I use this type in other files, which is why I could change it if asked.

userManager: UserManagerState;
signInCallbackError: Error | null;
authenticationRouterError: AuthenticationRouterErrorState | null;
showAuthenticationRouterLogin: boolean;
dispatch: Dispatch<AuthenticationActions>;
navigate: NavigateFunction;
location: Location;
}
2 changes: 1 addition & 1 deletion src/components/AuthenticationRouter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export type {
AuthenticationRouterErrorState,
AuthenticationRouterProps,
UserManagerState,
} from './AuthenticationRouter';
} from './authenticationType';
Loading