Skip to content

Commit

Permalink
Improve fix
Browse files Browse the repository at this point in the history
  • Loading branch information
WiXSL committed Jan 3, 2022
1 parent d6bc43b commit 2b18d08
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
5 changes: 2 additions & 3 deletions packages/ra-core/src/auth/useLogoutIfAccessDenied.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { useState, useEffect } from 'react';
import expect from 'expect';
import { waitFor } from '@testing-library/react';
import { render, waitFor } from '@testing-library/react';
import { Router } from 'react-router-dom';
import { createMemoryHistory } from 'history';

Expand All @@ -10,7 +10,6 @@ import AuthContext from './AuthContext';
import useLogout from './useLogout';
import useNotify from '../sideEffect/useNotify';
import { AuthProvider } from '../types';
import { renderWithRedux } from 'ra-test';

let loggedIn = true;

Expand Down Expand Up @@ -63,7 +62,7 @@ useNotify.mockImplementation(() => notify);

function renderInRouter(children) {
const history = createMemoryHistory();
const api = renderWithRedux(<Router history={history}>{children}</Router>);
const api = render(<Router history={history}>{children}</Router>);

return {
...api,
Expand Down
6 changes: 1 addition & 5 deletions packages/ra-core/src/auth/useLogoutIfAccessDenied.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import useAuthProvider from './useAuthProvider';
import useLogout from './useLogout';
import { useNotify } from '../sideEffect';
import { useHistory } from 'react-router';
import { fetchEnd } from '../actions';
import { useDispatch } from 'react-redux';

let timer;

Expand Down Expand Up @@ -45,7 +43,6 @@ const useLogoutIfAccessDenied = (): LogoutIfAccessDenied => {
const logout = useLogout();
const notify = useNotify();
const history = useHistory();
const dispatch = useDispatch();

const logoutIfAccessDenied = useCallback(
(error?: any, disableNotification?: boolean) =>
Expand Down Expand Up @@ -96,13 +93,12 @@ const useLogoutIfAccessDenied = (): LogoutIfAccessDenied => {
if (logoutUser) {
logout({}, redirectTo);
} else {
dispatch(fetchEnd());
history.push(redirectTo);
}

return true;
}),
[authProvider, logout, notify, history, dispatch]
[authProvider, logout, notify, history]
);
return authProvider
? logoutIfAccessDenied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export const performOptimisticQuery = ({
console.error(error);
}
return logoutIfAccessDenied(error).then(loggedOut => {
if (loggedOut) return;
if (loggedOut) {
dispatch({ type: FETCH_END });
return;
}
dispatch({
type: `${action}_FAILURE`,
error: error.message ? error.message : error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export const performPessimisticQuery = ({
console.error(error);
}
return logoutIfAccessDenied(error).then(loggedOut => {
if (loggedOut) return;
if (loggedOut) {
dispatch({ type: FETCH_END });
return;
}
dispatch({
type: `${action}_FAILURE`,
error: error.message ? error.message : error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export const performUndoableQuery = ({
console.error(error);
}
return logoutIfAccessDenied(error).then(loggedOut => {
if (loggedOut) return;
if (loggedOut) {
dispatch({ type: FETCH_END });
return;
}
dispatch({
type: `${action}_FAILURE`,
error: error.message ? error.message : error,
Expand Down

0 comments on commit 2b18d08

Please sign in to comment.