Skip to content

Commit dbf3a73

Browse files
committed
Refactor auto login to avoid setState in useEffect
The previous implementation triggered handleLogin inside a useEffect on mount, which caused a synchronous setState call and an additional render cycle. This approach is not recommended by React and is flagged by eslint as it can cause unnecessary rerenders. Auto login is now handled during render using a guarded condition to ensure it runs only once. Signed-off-by: Jasmina <jasmina.piric@secomind.com>
1 parent e9a8541 commit dbf3a73

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

frontend/src/pages/Login.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* SPDX-License-Identifier: Apache-2.0
1919
*/
2020

21-
import { useCallback, useEffect, useState } from "react";
21+
import { useCallback, useState } from "react";
2222
import { FormattedMessage, useIntl } from "react-intl";
2323
import { useLocation, useNavigate } from "react-router-dom";
2424
import Alert from "react-bootstrap/Alert";
@@ -52,6 +52,7 @@ const LoginPage = () => {
5252
const [validated, setValidated] = useState(false);
5353
const [errorFeedback, setErrorFeedback] = useState<React.ReactNode>(null);
5454
const [isLoggingIn, setIsLoggingIn] = useState(false);
55+
const [autoLoginTriggered, setAutoLoginTriggered] = useState(false);
5556
const auth = useAuth();
5657
const intl = useIntl();
5758
const navigate = useNavigate();
@@ -99,13 +100,14 @@ const LoginPage = () => {
99100
setFormData((data) => ({ ...data, [field]: value }));
100101
}, []);
101102

102-
useEffect(() => {
103-
if (initialFormData.tenantSlug && initialFormData.authToken) {
104-
handleLogin(initialFormData);
105-
}
106-
// Run once on mount
107-
// eslint-disable-next-line react-hooks/exhaustive-deps
108-
}, []);
103+
if (
104+
!autoLoginTriggered &&
105+
initialFormData.tenantSlug &&
106+
initialFormData.authToken
107+
) {
108+
setAutoLoginTriggered(true);
109+
handleLogin(initialFormData);
110+
}
109111

110112
return (
111113
<AuthPage>

0 commit comments

Comments
 (0)