import { useCallback } from 'react' ; import useAuthProvider from './useAuthProvider' ; import useLogout from './useLogout' ; import { useNotify } from '../sideEffect' ; import { useHistory } from 'react-router' ; let timer ; see useLogout see useDataProvider returns { Function } example const useLogoutIfAccessDenied = ( ) : LogoutIfAccessDenied => { const authProvider = useAuthProvider ( ) ; const logout = useLogout ( ) ; const notify = useNotify ( ) ; const history = useHistory ( ) ; const logoutIfAccessDenied = useCallback ( ( error ? , disableNotification ? ) => authProvider . checkError ( error ) . then ( ( ) => false ) . catch ( async ( e ) => { const logoutUser = e ?. logoutUser ?? true ; if ( timer ) { return true ; } timer = setTimeout ( ( ) => { timer = undefined ; } , 0 ) ; const shouldNotify = ! ( disableNotification || ( e && e . message === false ) || ( error && error . message === false ) ) ; if ( shouldNotify ) { authProvider . checkAuth ( { } ) . then ( ( ) => { if ( logoutUser ) { notify ( 'ra.notification.logged_out' , { type : 'warning' , } ) ; } else { notify ( 'ra.notification.not_authorized' , { type : 'warning' , } ) ; } } ) . catch ( ( ) => { } ) ; } const redirectTo = e && e . redirectTo ? e . redirectTo : error && error . redirectTo ? error . redirectTo : undefined ; if ( logoutUser ) { logout ( { } , redirectTo ) ; } else { history . push ( redirectTo ) ; } return true ; } ) , [ authProvider , logout , notify , history ] ) ; return authProvider ? logoutIfAccessDenied : logoutIfAccessDeniedWithoutProvider ; } ; const logoutIfAccessDeniedWithoutProvider = ( ) => Promise . resolve ( false ) ; param { Error } error param { boolean } disableNotification return { Promise } type LogoutIfAccessDenied = ( error ? : any , deprecated disableNotification ? : boolean ) => Promise < boolean > ; export default useLogoutIfAccessDenied ;