Skip to content

Commit

Permalink
Added onLogin prop to okta login screen
Browse files Browse the repository at this point in the history
  • Loading branch information
surajeaton committed Jul 26, 2024
1 parent ffdd791 commit 236e03e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import '@testing-library/jest-dom';
import { OktaLoginScreenBase } from './OktaLoginScreenBase';
import { useOktaAuth } from '@okta/okta-react';
import { Box } from '@mui/material';
import { ErrorContextProvider } from '../../contexts/ErrorContext';
import { OktaLoginScreenProps } from './types';
import { errorContextProviderProps } from '../../contexts/ErrorContext/ErrorContextProvider.test';

jest.mock('@okta/okta-react', () => ({
useOktaAuth: jest.fn(),
Expand All @@ -26,20 +24,15 @@ describe('OktaLoginScreenBase', () => {
});
});

const renderer = (props?: OktaLoginScreenProps): RenderResult =>
render(
<ErrorContextProvider {...errorContextProviderProps}>
<OktaLoginScreenBase {...props} />
</ErrorContextProvider>
);
const renderer = (props?: OktaLoginScreenProps): RenderResult => render(<OktaLoginScreenBase {...props} />);

it('renders correctly', () => {
renderer({ header: <Box data-testid="test-header">Test Header</Box> });
expect(screen.getByText('Test Header')).toBeInTheDocument();
});

it('calls signInWithRedirect on login button click', () => {
renderer({ loginButtonLabel: 'Login' });
renderer({ loginButtonLabel: 'Login', onLogin: mockSignInWithRedirect });
const loginButton = screen.getByText('Login');
fireEvent.click(loginButton);
expect(mockSignInWithRedirect).toHaveBeenCalled();
Expand Down
18 changes: 3 additions & 15 deletions login-workflow/src/screens/OktaLoginScreen/OktaLoginScreenBase.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React from 'react';
import { useOktaAuth } from '@okta/okta-react';
import { OktaLoginScreenProps } from './types';
import { WorkflowCard } from '../../components/WorkflowCard';
import { WorkflowCardBody } from '../../components/WorkflowCard/WorkflowCardBody';
import { Box, Button, Typography } from '@mui/material';
import ErrorManager from '../../components/Error/ErrorManager';
import { LinkStyles } from '../../styles';
import cyberSecurityBadge from '../../assets/images/cybersecurity_certified.png';
import { useErrorManager } from '../../contexts/ErrorContext/useErrorManager';
import { unstable_composeClasses as composeClasses } from '@mui/base';
import { getOktaLoginScreenUtilityClass, OktaLoginScreenClassKey } from './utilityClasses';

Expand Down Expand Up @@ -36,15 +34,14 @@ const useUtilityClasses = (ownerState: OktaLoginScreenProps): Record<OktaLoginSc
*/

export const OktaLoginScreenBase: React.FC<OktaLoginScreenProps> = (props) => {
const { authState, oktaAuth } = useOktaAuth();
const { triggerError } = useErrorManager();
const defaultClasses = useUtilityClasses(props);

const {
header,
projectImage,
errorDisplayConfig,
loginButtonLabel,
onLogin,
showContactSupport,
showCyberSecurityBadge,
contactSupportLabel,
Expand All @@ -54,24 +51,15 @@ export const OktaLoginScreenBase: React.FC<OktaLoginScreenProps> = (props) => {
} = props;

const handleOnLogin = async (): Promise<void> => {
try {
await oktaAuth.signInWithRedirect();
} catch (_error) {
triggerError(_error as Error);
}
if (onLogin) await onLogin();
};

const handleContactSupport = (): void => {
if (onContactSupport) onContactSupport();
};

return (
<WorkflowCard
loading={!authState}
className={defaultClasses.root}
data-testid={defaultClasses.root}
{...otherProps}
>
<WorkflowCard className={defaultClasses.root} data-testid={defaultClasses.root} {...otherProps}>
<WorkflowCardBody
sx={{
py: { xs: 4, sm: 4, md: 4 },
Expand Down
6 changes: 6 additions & 0 deletions login-workflow/src/screens/OktaLoginScreen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export type OktaLoginScreenProps = WorkflowCardBaseProps & {
*/
loginButtonLabel?: string;

/**
* Callback function that is called when the login button is clicked
* @returns Promise<void> | void
*/
onLogin?: () => Promise<void> | void;

/**
* whether or not to show the 'contact support' link
*/
Expand Down

0 comments on commit 236e03e

Please sign in to comment.