Skip to content

Commit b96be96

Browse files
committed
client: Fix CheckAuthorization redirection
The `CheckAuthorization` was no longer redirecting to the `/sign/in` page and the following warning was logged into the console: > You should call navigate() in a React.useEffect(), not when your component is first rendered. Probably regressed in 88deede. Let’s fix the redirection. Using polyfill for `useEffectEvent` from react 1.19.2 to get `eslint-plugin-react-hooks` of our back. Keeping the error message in the body to have some content in case the effect is slow.
1 parent 903d576 commit b96be96

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

client/js/templates/App.jsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useNavigate,
1111
useLocation,
1212
} from 'react-router';
13+
import { useEffectEvent } from 'use-effect-event';
1314
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
1415
import { Collapse } from '@kunukn/react-collapse';
1516
import classNames from 'classnames';
@@ -92,13 +93,23 @@ function NotFound() {
9293

9394
function CheckAuthorization({ isAllowed, returnLocation, _, children }) {
9495
const navigate = useNavigate();
96+
97+
const redirect = useEffectEvent(() => {
98+
navigate('/sign/in', {
99+
state: { returnLocation },
100+
});
101+
});
102+
103+
useEffect(() => {
104+
if (!isAllowed) {
105+
redirect();
106+
}
107+
}, [isAllowed]);
108+
95109
if (!isAllowed) {
96110
const [preLink, inLink, postLink] = _('error_unauthorized').split(
97111
/\{(?:link_begin|link_end)\}/,
98112
);
99-
navigate('/sign/in', {
100-
state: { returnLocation },
101-
});
102113

103114
return (
104115
<p>

client/package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"rooks": "^9.0.0",
2828
"tinykeys": "^3.0.0",
2929
"unreset-css": "^1.0.1",
30+
"use-effect-event": "^2.0.3",
3031
"use-state-with-deps": "^1.1.1",
3132
"yet-another-react-lightbox": "^3.5.0"
3233
},

0 commit comments

Comments
 (0)