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

Avoid cloning elements in Authenticated #7203

Merged
merged 3 commits into from
Feb 8, 2022
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
2 changes: 1 addition & 1 deletion packages/ra-core/src/auth/Authenticated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Routes, Route, useLocation } from 'react-router-dom';
import { memoryStore } from '../store';
import { CoreAdminContext } from '../core';
import { useNotificationContext } from '../notification';
import Authenticated from './Authenticated';
import { Authenticated } from './Authenticated';

describe('<Authenticated>', () => {
const Foo = () => <div>Foo</div>;
Expand Down
25 changes: 9 additions & 16 deletions packages/ra-core/src/auth/Authenticated.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { cloneElement, ReactElement } from 'react';
import * as React from 'react';
import { ReactNode } from 'react';

import { useAuthenticated } from './useAuthenticated';

export interface AuthenticatedProps {
children: ReactElement<any>;
authParams?: object;
location?: object; // kept for backwards compatibility, unused
}

/**
* Restrict access to children to authenticated users.
* Redirects anonymous users to the login page.
Expand Down Expand Up @@ -36,17 +31,15 @@ export interface AuthenticatedProps {
* </Admin>
* );
*/
const Authenticated = (props: AuthenticatedProps) => {
const {
authParams,
children,
location, // kept for backwards compatibility, unused
...rest
} = props;
export const Authenticated = (props: AuthenticatedProps) => {
const { authParams, children } = props;
useAuthenticated({ params: authParams });
// render the child even though the useAuthenticated() call isn't finished (optimistic rendering)
// the above hook will log out if the authProvider doesn't validate that the user is authenticated
return cloneElement(children, rest);
return <>{children}</>;
};

export default Authenticated;
export interface AuthenticatedProps {
children: ReactNode;
authParams?: object;
}
5 changes: 2 additions & 3 deletions packages/ra-core/src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Authenticated, { AuthenticatedProps } from './Authenticated';
import AuthContext from './AuthContext';
import useAuthProvider from './useAuthProvider';
import useAuthState from './useAuthState';
Expand All @@ -12,6 +11,7 @@ import useGetPermissions from './useGetPermissions';
import useLogoutIfAccessDenied from './useLogoutIfAccessDenied';
import convertLegacyAuthProvider from './convertLegacyAuthProvider';

export * from './Authenticated';
export * from './types';
export * from './useAuthenticated';
export * from './useCheckAuth';
Expand All @@ -32,8 +32,7 @@ export {
// hook with immediate effect
useLogoutIfAccessDenied,
// components
Authenticated,
WithPermissions,
};

export type { AuthenticatedProps, WithPermissionsProps };
export type { WithPermissionsProps };
2 changes: 1 addition & 1 deletion packages/ra-core/src/auth/useAuthenticated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createMemoryHistory } from 'history';
import { Routes, Route, useLocation } from 'react-router-dom';

import { memoryStore } from '../store';
import Authenticated from './Authenticated';
import { Authenticated } from './Authenticated';
import { useNotificationContext } from '../notification';
import { CoreAdminContext } from '../core';

Expand Down