From ecc5f6bba3b6bf383a5206134f22e98b3d3997b9 Mon Sep 17 00:00:00 2001 From: reedwane Date: Mon, 11 Nov 2024 10:52:17 +0100 Subject: [PATCH 1/3] refactor(core): allow passing optional check params to Authenticated component --- .changeset/orange-eels-bake.md | 7 +++ .../components/authenticated/index.spec.tsx | 43 +++++++++++++++++++ .../src/components/authenticated/index.tsx | 12 +++++- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 .changeset/orange-eels-bake.md diff --git a/.changeset/orange-eels-bake.md b/.changeset/orange-eels-bake.md new file mode 100644 index 000000000000..084eb8b4b857 --- /dev/null +++ b/.changeset/orange-eels-bake.md @@ -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 diff --git a/packages/core/src/components/authenticated/index.spec.tsx b/packages/core/src/components/authenticated/index.spec.tsx index b02273693a86..52ac53db910c 100644 --- a/packages/core/src/components/authenticated/index.spec.tsx +++ b/packages/core/src/components/authenticated/index.spec.tsx @@ -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( + + Custom 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" }; + }, + }, + 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", + }), + ), + ); + }); }); diff --git a/packages/core/src/components/authenticated/index.tsx b/packages/core/src/components/authenticated/index.tsx index 02233585e2ee..62a2201ddf21 100644 --- a/packages/core/src/components/authenticated/index.tsx +++ b/packages/core/src/components/authenticated/index.tsx @@ -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. @@ -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 = { @@ -97,6 +103,7 @@ export function Authenticated({ children, fallback: fallbackContent, loading: loadingContent, + params, }: AuthenticatedProps | LegacyAuthenticatedProps): JSX.Element | null { const activeAuthProvider = useActiveAuthProvider(); const routerType = useRouterType(); @@ -119,6 +126,7 @@ export function Authenticated({ } = {}, } = useIsAuthenticated({ v3LegacyAuthProviderCompatible: isLegacyAuth, + params, }); // Authentication status @@ -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) From c95d8270a1751ed43c21fd317ab8deb5a15b5873 Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Tue, 3 Dec 2024 11:10:39 +0300 Subject: [PATCH 2/3] feat(docs): add docs for Authenticated component --- .../authentication/components/authenticated/index.md | 10 ++++++++++ packages/core/src/components/authenticated/index.tsx | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/documentation/docs/authentication/components/authenticated/index.md b/documentation/docs/authentication/components/authenticated/index.md index 688d8b325cdc..29e6c74386e3 100644 --- a/documentation/docs/authentication/components/authenticated/index.md +++ b/documentation/docs/authentication/components/authenticated/index.md @@ -84,6 +84,16 @@ Component to render while checking whether the user is logged in. ``` +### params + +Additional params to be passed to Auth Provider's `check` method via `useIsAuthenticated` hook. + +```tsx + + + +``` + ## API Reference ### Properties diff --git a/packages/core/src/components/authenticated/index.tsx b/packages/core/src/components/authenticated/index.tsx index 62a2201ddf21..dbca0d2d0edc 100644 --- a/packages/core/src/components/authenticated/index.tsx +++ b/packages/core/src/components/authenticated/index.tsx @@ -46,11 +46,11 @@ export type AuthenticatedCommonProps = { */ loading?: React.ReactNode; /** - * Content to show if user is logged in + * Content to show if user is logged in. */ children?: React.ReactNode; /** - * optional params to pass to the auth check via the useIsAuthenticated hook + * optional params to be passed to the Auth Provider's check method via the useIsAuthenticated hook. */ params?: AuthCheckParams; }; @@ -166,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) From b16c179723a6eebe8256bc5a2bc808506bd617ca Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Tue, 3 Dec 2024 11:29:17 +0300 Subject: [PATCH 3/3] chore: use minor --- .changeset/orange-eels-bake.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/orange-eels-bake.md b/.changeset/orange-eels-bake.md index 084eb8b4b857..9bd6a1ed901b 100644 --- a/.changeset/orange-eels-bake.md +++ b/.changeset/orange-eels-bake.md @@ -1,5 +1,5 @@ --- -"@refinedev/core": patch +"@refinedev/core": minor --- refactor: modified the Authenticated component to receive optional params prop to be passed to the useIsAuthenticated hook.