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

Fix missing session state on browser reload. #1282

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 14 additions & 9 deletions packages/react-utils/src/helpers/componentUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,11 @@ interface ComponentProps extends Partial<RouteProps> {

export const PrivateComponent = (props: ComponentProps) => {
// props to pass on to Connected Private Route
const { permissions, ...otherProps } = props;
const CPRProps = {
...otherProps,
keycloakBaseURL: configs.keycloakBaseURL,
opensrpBaseURL: configs.opensrpBaseURL,
fhirBaseURL: configs.fhirBaseURL,
};
const { permissions, component: WrappedComponent, ...otherProps } = props;

const { t } = useTranslation();

return (
const RbacWrappedComponent = (props: Record<string, unknown>) => (
<RbacCheck
permissions={permissions}
fallback={
Expand All @@ -49,9 +44,19 @@ export const PrivateComponent = (props: ComponentProps) => {
/>
}
>
<ConnectedPrivateRoute {...CPRProps} />
<WrappedComponent {...props} />
</RbacCheck>
);

const CPRProps = {
...otherProps,
component: RbacWrappedComponent,
keycloakBaseURL: configs.keycloakBaseURL,
opensrpBaseURL: configs.opensrpBaseURL,
fhirBaseURL: configs.fhirBaseURL,
};

return <ConnectedPrivateRoute {...CPRProps} />;
};

/**
Expand Down
31 changes: 31 additions & 0 deletions packages/react-utils/src/helpers/tests/componentUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import flushPromises from 'flush-promises';
import fetch from 'jest-fetch-mock';
import { superUserRole } from '../test-utils';
import { RbacProvider, RoleContext, UserRole } from '@opensrp/rbac';
import { render } from '@testing-library/react';
import { createMemoryHistory } from 'history';

const { PublicComponent, PrivateComponent, isAuthorized } = componentUtils;

Expand Down Expand Up @@ -44,6 +46,35 @@ describe('componentUtils', () => {
wrapper.unmount();
});

it('First check that user is logged in before Rbac', async () => {
const MockComponent = () => {
return <p>I love oof!</p>;
};
const history = createMemoryHistory();
const props = {
component: MockComponent,
redirectPath: '/login',
disableLoginProtection: false,
};

render(
<Provider store={store}>
<Router history={history}>
<RbacProvider value={superUserRole}>
<PrivateComponent {...props} component={MockComponent} permissions={[]} />
</RbacProvider>
</Router>
</Provider>
);
await act(async () => {
await flushPromises();
});

// should redirect non-AuthN'd users to login
expect(history.location.pathname).toEqual('/login');
expect(history.location.search).toEqual('?next=');
});

it('PrivateComponent Renders correctly', async () => {
store.dispatch(
authenticateUser(
Expand Down