diff --git a/web/src/components/core/LoginPage.jsx b/web/src/components/core/LoginPage.jsx index c04c8301e9..c0629dd15f 100644 --- a/web/src/components/core/LoginPage.jsx +++ b/web/src/components/core/LoginPage.jsx @@ -26,7 +26,7 @@ import { Navigate } from "react-router-dom"; import { ActionGroup, Button, Form, FormGroup } from "@patternfly/react-core"; import { sprintf } from "sprintf-js"; import { _ } from "~/i18n"; -import { useAuth } from "~/context/auth"; +import { AuthErrors, useAuth } from "~/context/auth"; import { About, FormValidationError, If, Page, PasswordInput, Section } from "~/components/core"; import { Center } from "~/components/layout"; @@ -39,14 +39,21 @@ import { Center } from "~/components/layout"; export default function LoginPage() { const [password, setPassword] = useState(""); const [error, setError] = useState(false); - const { isLoggedIn, login: loginFn } = useAuth(); + const { isLoggedIn, login: loginFn, error: loginError } = useAuth(); const login = async (e) => { e.preventDefault(); const result = await loginFn(password); - setError(!result); + + setError(result.status !== 200); }; + const errorMessage = (authError) => { + if (authError === AuthErrors.AUTH) + return _("Could not log in. Please, make sure that the password is correct."); + + return _("Could not authenticate against the server, please check it."); + }; if (isLoggedIn) { return ; } @@ -83,7 +90,7 @@ export default function LoginPage() { condition={error} then={ } /> diff --git a/web/src/context/auth.jsx b/web/src/context/auth.jsx index cc4f5389a7..74c528c721 100644 --- a/web/src/context/auth.jsx +++ b/web/src/context/auth.jsx @@ -37,12 +37,19 @@ function useAuth() { return context; } +const AuthErrors = Object.freeze({ + SERVER: "server", + AUTH: "auth", + OTHER: "other" +}); + /** * @param {object} props * @param {React.ReactNode} [props.children] - content to display within the provider */ function AuthProvider({ children }) { const [isLoggedIn, setIsLoggedIn] = useState(false); + const [error, setError] = useState(null); const login = useCallback(async (password) => { const response = await fetch("/api/auth", { @@ -50,9 +57,17 @@ function AuthProvider({ children }) { body: JSON.stringify({ password }), headers: { "Content-Type": "application/json" }, }); + const result = response.status === 200; + if ((response.status >= 500) && (response.status < 600)) { + setError(AuthErrors.SERVER); + } + if ((response.status >= 400) && (response.status < 500)) { + setError(AuthErrors.AUTH); + } setIsLoggedIn(result); - return result; + + return response; }, []); const logout = useCallback(async () => { @@ -68,15 +83,21 @@ function AuthProvider({ children }) { }) .then((response) => { setIsLoggedIn(response.status === 200); + if ((response.status >= 500) && (response.status < 600)) { + setError(AuthErrors.SERVER); + } + if ((response.status >= 400) && (response.status < 500)) { + setError(AuthErrors.AUTH); + } }) .catch(() => setIsLoggedIn(false)); }, []); return ( - + {children} ); } -export { AuthProvider, useAuth }; +export { AuthProvider, useAuth, AuthErrors };