From c29b76cc71f9b5acd15fecf381021c24e09d0b7f Mon Sep 17 00:00:00 2001 From: mufazalov Date: Tue, 8 Oct 2024 17:32:42 +0300 Subject: [PATCH] fix(Authentication): handle login error properly --- .../Authentication/Authentication.tsx | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/containers/Authentication/Authentication.tsx b/src/containers/Authentication/Authentication.tsx index bcc67fa65e..fbee833cf1 100644 --- a/src/containers/Authentication/Authentication.tsx +++ b/src/containers/Authentication/Authentication.tsx @@ -24,7 +24,7 @@ function Authentication({closable = false}: AuthenticationProps) { const history = useHistory(); const location = useLocation(); - const [authenticate, {error, isLoading}] = authenticationApi.useAuthenticateMutation(undefined); + const [authenticate, {isLoading}] = authenticationApi.useAuthenticateMutation(undefined); const {returnUrl} = parseQuery(location); @@ -34,15 +34,6 @@ function Authentication({closable = false}: AuthenticationProps) { const [passwordError, setPasswordError] = React.useState(''); const [showPassword, setShowPassword] = React.useState(false); - React.useEffect(() => { - if (isUserError(error)) { - setLoginError(error.data.error); - } - if (isPasswordError(error)) { - setPasswordError(error.data.error); - } - }, [error]); - const onLoginUpdate = (value: string) => { setLogin(value); setLoginError(''); @@ -54,18 +45,28 @@ function Authentication({closable = false}: AuthenticationProps) { }; const onLoginClick = () => { - authenticate({user: login, password}).then(() => { - if (returnUrl) { - const decodedUrl = decodeURIComponent(returnUrl.toString()); - - // to prevent page reload we use router history - // history navigates relative to origin - // so we remove origin to make it work properly - const url = new URL(decodedUrl); - const path = url.pathname + url.search; - history.replace(path); - } - }); + authenticate({user: login, password}) + .unwrap() + .then(() => { + if (returnUrl) { + const decodedUrl = decodeURIComponent(returnUrl.toString()); + + // to prevent page reload we use router history + // history navigates relative to origin + // so we remove origin to make it work properly + const url = new URL(decodedUrl); + const path = url.pathname + url.search; + history.replace(path); + } + }) + .catch((error) => { + if (isUserError(error)) { + setLoginError(error.data.error); + } + if (isPasswordError(error)) { + setPasswordError(error.data.error); + } + }); }; const onEnterClick = (e: React.KeyboardEvent) => {