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

refactor(core): allow passing optional check params to Authenticated … #6483

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions .changeset/orange-eels-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/core": patch
---

refactor: modified the Authenticated component to receive optional params prop to be passed to the useIsAuthenticated hook.

Fixes #6309
43 changes: 43 additions & 0 deletions packages/core/src/components/authenticated/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,47 @@ describe("Authenticated", () => {
),
);
});

it("should not authenticate and redirect if params indicate failed authentication", async () => {
const mockGo = jest.fn();
const mockParams = { allowAuth: false }; // Parameter that simulates failed authentication

const { queryByText } = render(
<Authenticated key="auth-with-params" params={mockParams}>
Custom Authenticated
</Authenticated>,
{
wrapper: TestWrapper({
dataProvider: MockJSONServer,
authProvider: {
...mockAuthProvider,
check: async ({ params }) => {
// Simulate failed authentication based on params
return params?.allowAuth
? { authenticated: true }
: { authenticated: false, redirectTo: "/login" };
},
Comment on lines +623 to +628
Copy link
Member

Choose a reason for hiding this comment

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

Optionally, this can be simplified. Passing a mockCheck here and assert that mockCheck is called with given mockParams. But this also works because you are checking if redirect is called, so it's a matter of taste.

},
routerProvider: {
go: () => mockGo,
},
resources: [{ name: "posts", route: "posts" }],
}),
},
);

// Since requireAuth is false, the component should redirect to /login
await act(async () => {
expect(queryByText("Custom Authenticated")).toBeNull();
});

await waitFor(() =>
expect(mockGo).toBeCalledWith(
expect.objectContaining({
to: "/login",
type: "replace",
}),
),
);
});
});
12 changes: 10 additions & 2 deletions packages/core/src/components/authenticated/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from "@hooks";
import type { GoConfig } from "../../contexts/router/types";

export type AuthCheckParams = any;

export type AuthenticatedCommonProps = {
/**
* Unique key to identify the component.
Expand Down Expand Up @@ -47,6 +49,10 @@ export type AuthenticatedCommonProps = {
* Content to show if user is logged in
*/
children?: React.ReactNode;
/**
* optional params to pass to the auth check via the useIsAuthenticated hook
*/
params?: AuthCheckParams;
};

export type LegacyAuthenticatedProps = {
Expand Down Expand Up @@ -97,6 +103,7 @@ export function Authenticated({
children,
fallback: fallbackContent,
loading: loadingContent,
params,
}: AuthenticatedProps | LegacyAuthenticatedProps): JSX.Element | null {
const activeAuthProvider = useActiveAuthProvider();
const routerType = useRouterType();
Expand All @@ -119,6 +126,7 @@ export function Authenticated({
} = {},
} = useIsAuthenticated({
v3LegacyAuthProviderCompatible: isLegacyAuth,
params,
});

// Authentication status
Expand Down Expand Up @@ -158,8 +166,8 @@ export function Authenticated({
? redirectOnFail
: "/login"
: typeof redirectOnFail === "string"
? redirectOnFail
: (authenticatedRedirect as string | undefined);
? redirectOnFail
: (authenticatedRedirect as string | undefined);

// Current pathname to append to the redirect url.
// User will be redirected to this url after successful mutation. (like login)
Expand Down
Loading