Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ import { withAuthenticationRequired } from "react-oidc-context";
const PrivateRoute = () => (<div>Private</div>);

export default withAuthenticationRequired(PrivateRoute, {
OnRedirecting: () => (<div>Redirecting to the login page...</div>)
onRedirecting: () => (<div>Redirecting to the login page...</div>)
});
```

Expand Down
2 changes: 2 additions & 0 deletions docs/react-oidc-context.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ export const withAuthenticationRequired: <P extends object>(Component: React_2.C
// @public (undocumented)
export interface WithAuthenticationRequiredProps {
onBeforeSignin?: () => Promise<void> | void;
// @deprecated
OnRedirecting?: () => React_2.JSX.Element;
onRedirecting?: () => React_2.JSX.Element;
signinRedirectArgs?: SigninRedirectArgs;
}

Expand Down
12 changes: 10 additions & 2 deletions src/withAuthenticationRequired.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export interface WithAuthenticationRequiredProps {
/**
* Show a message when redirected to the signin page.
*/
onRedirecting?: () => React.JSX.Element;

/**
* Show a message when redirected to the signin page.
* @deprecated Use `onRedirecting` instead.
*/
OnRedirecting?: () => React.JSX.Element;

/**
Expand All @@ -35,7 +41,7 @@ export const withAuthenticationRequired = <P extends object>(
Component: React.ComponentType<P>,
options: WithAuthenticationRequiredProps = {},
): React.FC<P> => {
const { OnRedirecting = (): React.JSX.Element => <></>, onBeforeSignin, signinRedirectArgs } = options;
const { onRedirecting, OnRedirecting, onBeforeSignin, signinRedirectArgs } = options;
const displayName = `withAuthenticationRequired(${Component.displayName || Component.name})`;
const C: React.FC<P> = (props) => {
const auth = useAuth();
Expand All @@ -51,7 +57,9 @@ export const withAuthenticationRequired = <P extends object>(
})();
}, [auth.isLoading, auth.isAuthenticated, auth]);

return auth.isAuthenticated ? <Component {...props} /> : OnRedirecting();
const renderRedirecting = onRedirecting || OnRedirecting || ((): React.JSX.Element => <></>);

return auth.isAuthenticated ? <Component {...props} /> : renderRedirecting();
};

C.displayName = displayName;
Expand Down
27 changes: 27 additions & 0 deletions test/withAuthenticationRequired.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ describe("withAuthenticationRequired", () => {
authContext.signinRedirect = signinRedirectMock;
useAuthMock.mockReturnValue(authContext);

const MyComponent = (): React.JSX.Element => <>Private</>;
const onRedirecting = (): React.JSX.Element => <>Redirecting</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
onRedirecting,
});

// act
act(() => {
render(
<AuthProvider {...settingsStub}>
<WrappedComponent />
</AuthProvider>,
);
});

// assert
await screen.findByText("Redirecting");
});

it("should show a custom redirecting message when not authenticated (using deprecated OnRedirecting)", async () => {
// arrange
const useAuthMock = jest.spyOn(useAuthModule, "useAuth");
const authContext = { isLoading: false, isAuthenticated: false } as AuthContextProps;
const signinRedirectMock = jest.fn().mockResolvedValue(undefined);
authContext.signinRedirect = signinRedirectMock;
useAuthMock.mockReturnValue(authContext);

const MyComponent = (): React.JSX.Element => <>Private</>;
const OnRedirecting = (): React.JSX.Element => <>Redirecting</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
Expand Down