Skip to content

Commit 0871934

Browse files
committed
refactor: avoid synchronous state updates in useEffect
Fixes a cascading render warning caused by calling `handleLogin` synchronously inside a mounting effect. Changes: - Decoupled the API login logic from the manual UI loading trigger. - Initialized `isLoggingIn` state based on URL parameters to prevent an immediate rerender on mount. - Extracted `performLogin` to handle the side effect without triggering synchronous state updates. Signed-off-by: Jasmina <jasmina.piric@secomind.com>
1 parent e9a8541 commit 0871934

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

frontend/src/pages/Login.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of Edgehog.
33
*
4-
* Copyright 2021-2025 SECO Mind Srl
4+
* Copyright 2021-2026 SECO Mind Srl
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -48,17 +48,19 @@ const LoginPage = () => {
4848
const urlSearchParams = new URLSearchParams(location.search);
4949
const initialFormData = getInitialFormData(urlSearchParams);
5050
const redirectTo = urlSearchParams.get("redirectTo") || "";
51+
const shouldAutoLogin = Boolean(
52+
initialFormData.tenantSlug && initialFormData.authToken,
53+
);
5154
const [formData, setFormData] = useState<FormData>(initialFormData);
5255
const [validated, setValidated] = useState(false);
5356
const [errorFeedback, setErrorFeedback] = useState<React.ReactNode>(null);
54-
const [isLoggingIn, setIsLoggingIn] = useState(false);
57+
const [isLoggingIn, setIsLoggingIn] = useState(shouldAutoLogin);
5558
const auth = useAuth();
5659
const intl = useIntl();
5760
const navigate = useNavigate();
5861

5962
const handleLogin = useCallback(
6063
(formData: FormData) => {
61-
setIsLoggingIn(true);
6264
const session = _.pick(formData, ["tenantSlug", "authToken"]);
6365
const persistConfig = formData.keepMeLoggedIn;
6466
auth.login(session, persistConfig).then((success) => {
@@ -86,6 +88,8 @@ const LoginPage = () => {
8688
if (form.checkValidity() === false) {
8789
return setValidated(true);
8890
}
91+
92+
setIsLoggingIn(true);
8993
handleLogin({ ...formData, authToken: formData.authToken.trim() });
9094
},
9195
[formData, handleLogin],
@@ -100,7 +104,7 @@ const LoginPage = () => {
100104
}, []);
101105

102106
useEffect(() => {
103-
if (initialFormData.tenantSlug && initialFormData.authToken) {
107+
if (shouldAutoLogin) {
104108
handleLogin(initialFormData);
105109
}
106110
// Run once on mount

0 commit comments

Comments
 (0)