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

[TypeScript] Make types more strict in ra-core, part III #9760

Merged
merged 15 commits into from
Apr 9, 2024
6 changes: 2 additions & 4 deletions packages/ra-core/src/auth/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AuthProvider, UserIdentity } from '../types';

const defaultIdentity: UserIdentity = { id: '' };

const defaultProvider: AuthProvider = {
export const defaultAuthProvider: AuthProvider = {
login: () => Promise.resolve(),
logout: () => Promise.resolve(),
checkAuth: () => Promise.resolve(),
Expand All @@ -13,8 +13,6 @@ const defaultProvider: AuthProvider = {
getIdentity: () => Promise.resolve(defaultIdentity),
};

const AuthContext = createContext<AuthProvider>(defaultProvider);
export const AuthContext = createContext<AuthProvider>(defaultAuthProvider);

AuthContext.displayName = 'AuthContext';

export default AuthContext;
3 changes: 1 addition & 2 deletions packages/ra-core/src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AuthContext from './AuthContext';
import useAuthProvider from './useAuthProvider';
import useAuthState from './useAuthState';
import usePermissions from './usePermissions';
Expand All @@ -10,6 +9,7 @@ import useLogoutIfAccessDenied from './useLogoutIfAccessDenied';
import convertLegacyAuthProvider from './convertLegacyAuthProvider';

export * from './Authenticated';
export * from './AuthContext';
export * from './types';
export * from './useAuthenticated';
export * from './useCheckAuth';
Expand All @@ -19,7 +19,6 @@ export * from './addRefreshAuthToAuthProvider';
export * from './addRefreshAuthToDataProvider';

export {
AuthContext,
useAuthProvider,
convertLegacyAuthProvider,
// low-level hooks for calling a particular verb on the authProvider
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/auth/useAuthProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from 'react';

import { AuthProvider } from '../types';
import AuthContext from './AuthContext';
import { AuthContext } from './AuthContext';

export const defaultAuthParams = {
loginUrl: '/login',
Expand Down
14 changes: 10 additions & 4 deletions packages/ra-core/src/core/CoreAdminContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { useMemo } from 'react';
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';

import { AdminRouter } from '../routing';
import { AuthContext, convertLegacyAuthProvider } from '../auth';
import {
AuthContext,
defaultAuthProvider,
convertLegacyAuthProvider,
} from '../auth';
import {
DataProviderContext,
convertLegacyDataProvider,
Expand Down Expand Up @@ -187,9 +191,11 @@ React-admin requires a valid dataProvider function to work.`);

const finalAuthProvider = useMemo(
() =>
authProvider instanceof Function
? convertLegacyAuthProvider(authProvider)
: authProvider,
authProvider
? authProvider instanceof Function
? convertLegacyAuthProvider(authProvider)
: authProvider
: defaultAuthProvider,
Comment on lines +194 to +198
Copy link
Contributor

Choose a reason for hiding this comment

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

Just found out this is a BC, because now all admins have a default non-null authProvider.
So this breaks code like the following:

        <Root
            label={
                authProvider != null
                    ? 'ra.auth.user_menu'
                    : 'ra.configurable.customize'
            }
            icon={
                authProvider ? (
                    <Avatar src={identity.avatar} />
                ) : (
                    <SettingsIcon />
                )
            }
            {...props}
        />

Is there a way to avoid it?

Copy link
Contributor

Choose a reason for hiding this comment

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

#9861 attempts to fix this BC

[authProvider]
);

Expand Down