Skip to content

Commit 27e9590

Browse files
committed
Fix useCheckAuth fails when dataProvider.checkAuth is nto async
1 parent ee2d8e1 commit 27e9590

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

packages/ra-core/src/auth/useCheckAuth.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,43 @@ export const useCheckAuth = (): CheckAuth => {
5151
`${basename}/${defaultAuthParams.loginUrl}`
5252
);
5353

54-
const checkAuth = useCallback(
55-
(params: any = {}, logoutOnFailure = true, redirectTo = loginUrl) =>
56-
authProvider
57-
? authProvider.checkAuth(params).catch(error => {
58-
if (logoutOnFailure) {
59-
logout(
60-
{},
61-
error && error.redirectTo != null
62-
? error.redirectTo
63-
: redirectTo
64-
);
65-
const shouldSkipNotify =
66-
error && error.message === false;
67-
!shouldSkipNotify &&
68-
notify(
69-
getErrorMessage(
70-
error,
71-
'ra.auth.auth_check_error'
72-
),
73-
{ type: 'error' }
74-
);
75-
}
76-
throw error;
77-
})
78-
: checkAuthWithoutAuthProvider(),
54+
const checkAuth = useCallback<CheckAuth>(
55+
async (
56+
params: any = {},
57+
logoutOnFailure = true,
58+
redirectTo = loginUrl
59+
) => {
60+
// The authProvider is optional in react-admin
61+
if (!authProvider) {
62+
return checkAuthWithoutAuthProvider();
63+
}
64+
try {
65+
return await authProvider.checkAuth(params);
66+
} catch (error: any) {
67+
if (logoutOnFailure) {
68+
logout(
69+
{},
70+
error && error.redirectTo != null
71+
? error.redirectTo
72+
: redirectTo
73+
);
74+
const shouldSkipNotify = error && error.message === false;
75+
!shouldSkipNotify &&
76+
notify(
77+
getErrorMessage(error, 'ra.auth.auth_check_error'),
78+
{ type: 'error' }
79+
);
80+
}
81+
throw error;
82+
}
83+
},
7984
[authProvider, logout, notify, loginUrl]
8085
);
8186

8287
return checkAuth;
8388
};
8489

85-
const checkAuthWithoutAuthProvider = () => Promise.resolve();
90+
const checkAuthWithoutAuthProvider = async () => undefined;
8691

8792
/**
8893
* Check if the current user is authenticated by calling authProvider.checkAuth().

0 commit comments

Comments
 (0)