Skip to content

Commit

Permalink
fix: ORV2-1808 Sonarcloud fixes (part 3) (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnan-aot authored Dec 26, 2023
1 parent 76aefc7 commit e9f4215
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 106 deletions.
93 changes: 47 additions & 46 deletions frontend/src/common/authentication/Authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,58 @@ import { Box, Container, Typography } from "@mui/material";
import { Loading } from "../pages/Loading";
import { IDPS } from "../types/idp";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const LoginOptions = ({ auth }: any) => (
<>
<Box sx={{ margin: "8px" }}>
<Button
id="login-bceid"
variant="contained"
onClick={() => {
auth.signinRedirect({
extraQueryParams: { kc_idp_hint: IDPS.BCEID },
});
}}
sx={{ width: "200px" }}
>
Log in with BCeID
</Button>
</Box>
<Box sx={{ margin: "8px" }}>
<Button
id="login-idir"
variant="contained"
onClick={() => {
auth.signinRedirect({
extraQueryParams: { kc_idp_hint: IDPS.IDIR },
});
}}
sx={{ width: "200px" }}
>
Log in with IDIR
</Button>
</Box>
</>
);
const LoginOptions = () => {
const { signinRedirect } = useAuth();
return (
<>
<Box sx={{ margin: "8px" }}>
<Button
id="login-bceid"
variant="contained"
onClick={() => {
signinRedirect({
extraQueryParams: { kc_idp_hint: IDPS.BCEID },
});
}}
sx={{ width: "200px" }}
>
Log in with BCeID
</Button>
</Box>
<Box sx={{ margin: "8px" }}>
<Button
id="login-idir"
variant="contained"
onClick={() => {
signinRedirect({
extraQueryParams: { kc_idp_hint: IDPS.IDIR },
});
}}
sx={{ width: "200px" }}
>
Log in with IDIR
</Button>
</Box>
</>
);
};

const RenderAuth = () => {
const auth = useAuth();
if (auth.isLoading) {
return <Loading />;
} else if (auth.isAuthenticated) {
return <LoginRedirect />;
}

return <LoginOptions />;
};

/*
* The Authentication component handles user login
*
*/
export const Authentication = () => {
const auth = useAuth();

const RenderAuth = () => {
if (auth.isLoading) {
return <Loading />;
} else if (auth.isAuthenticated) {
return <LoginRedirect />;
}

return <LoginOptions auth={auth} />;
};

return (
<Container
className="feature-container"
Expand All @@ -68,7 +69,7 @@ export const Authentication = () => {
onRouteBC
</Typography>

{RenderAuth()}
<RenderAuth />
</Container>
);
};
97 changes: 48 additions & 49 deletions frontend/src/common/authentication/LoginRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,52 @@ import {
} from "../../routes/constants";
import { Optional } from "../types/common";

const navigateBCeID = (
userContextData: BCeIDUserContextType,
): string | undefined => {
const { associatedCompanies, pendingCompanies, migratedClient, user } =
userContextData;
// If the user does not exist
if (!user?.userGUID) {
// The user is in pending companies => Redirect them to User Info Page.
// i.e., The user has been invited.
if (pendingCompanies?.length > 0) {
return CREATE_PROFILE_WIZARD_ROUTES.WELCOME;
}
// The user and company do not exist (not a migrated client)
// => Redirect them to the welcome page with challenge.
else if (associatedCompanies?.length < 1 && !migratedClient?.clientNumber) {
return CREATE_PROFILE_WIZARD_ROUTES.WELCOME;
}
// The user does not exist but the business guid matches a migrated client.
// => Take them to no challenge workflow.
else if (migratedClient?.clientNumber) {
return CREATE_PROFILE_WIZARD_ROUTES.WELCOME;
}
// The user does not exist but there is one or more associated companies
// due to business GUID match. This is an error scenario and the user is unauthorized.

// Simply put, if !user and associatedCompanies.length > 0, get the guy out of here.
else {
return ERROR_ROUTES.UNAUTHORIZED;
}
}
// The user and company exist
else if (associatedCompanies?.length) {
return APPLICATIONS_ROUTES.BASE;
}
// User exists but company does not exist. This is not a possible scenario.
else if (!associatedCompanies?.length) {
// Error Page
return ERROR_ROUTES.UNAUTHORIZED;
}

// else if(pendingCompanies?.length) (i.e., user exists and has invites from a company)
// is not a valid block currently because
// one user can only be part of one company currently.
// -----------------------------
};

/*
* Redirects user to their correct page after loading their
* user and company info.
Expand All @@ -39,55 +85,8 @@ export const LoginRedirect = () => {
} else {
const userContextData: Optional<BCeIDUserContextType> =
queryClient.getQueryData<BCeIDUserContextType>(["userContext"]);
if (userContextData) {
const {
associatedCompanies,
pendingCompanies,
migratedClient,
user,
} = userContextData;
// If the user does not exist
if (!user?.userGUID) {
// The user is in pending companies => Redirect them to User Info Page.
// i.e., The user has been invited.
if (pendingCompanies?.length > 0) {
navigate(CREATE_PROFILE_WIZARD_ROUTES.WELCOME);
}
// The user and company do not exist (not a migrated client)
// => Redirect them to the welcome page with challenge.
else if (
associatedCompanies?.length < 1 &&
!migratedClient?.clientNumber
) {
navigate(CREATE_PROFILE_WIZARD_ROUTES.WELCOME);
}
// The user does not exist but the business guid matches a migrated client.
// => Take them to no challenge workflow.
else if (migratedClient?.clientNumber) {
navigate(CREATE_PROFILE_WIZARD_ROUTES.WELCOME);
}
// The user does not exist but there is one or more associated companies
// due to business GUID match. This is an error scenario and the user is unauthorized.

// Simply put, if !user and associatedCompanies.length > 0, get the guy out of here.
else {
navigate(ERROR_ROUTES.UNAUTHORIZED);
}
}
// The user and company exist
else if (associatedCompanies?.length) {
navigate(APPLICATIONS_ROUTES.BASE);
}
// User exists but company does not exist. This is not a possible scenario.
else if (!associatedCompanies?.length) {
// Error Page
navigate(ERROR_ROUTES.UNAUTHORIZED);
}

// else if(pendingCompanies?.length) (i.e., user exists and has invites from a company)
// is not a valid block currently because
// one user can only be part of one company currently.
}
const to = navigateBCeID(userContextData as BCeIDUserContextType);
navigate(to ?? ERROR_ROUTES.UNEXPECTED);
}
}
}, [isLoading]);
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/common/components/form/CustomFormComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ export const CustomFormComponent = <T extends ORBC_FormTypes>({
<CustomDatePicker
feature={feature}
name={name}
rules={rules}
inputProps={inputProps}
invalid={invalid}
disabled={disabled}
readOnly={readOnly}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import {
FieldValues,
FieldPath,
RegisterOptions,
ControllerRenderProps,
Path,
useFormContext,
useController,
} from "react-hook-form";
Expand All @@ -25,10 +22,6 @@ import { now } from "../../../helpers/formatDate";
export interface CustomDatePickerProps<T extends FieldValues> {
feature: string;
name: FieldPath<T>;
rules: RegisterOptions;
inputProps: RegisterOptions;
invalid: boolean;
field?: ControllerRenderProps<FieldValues, Path<T>>;
disabled?: boolean;
readOnly?: boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/features/permits/components/list/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const List = memo(
const newObject: { [key: string]: boolean } = {};
// Setting the selected row to false so that
// the row appears unchecked.
newObject[row.original.permitId as string] = false;
newObject[row.original.permitId] = false;
return newObject;
});
}}
Expand Down

0 comments on commit e9f4215

Please sign in to comment.