Skip to content

Commit f491578

Browse files
committed
Improve login page error handling.
1 parent 9618a5f commit f491578

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

frontend/src/components/auth/Login.tsx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
FormLabel,
88
HStack,
99
Input,
10+
Spinner,
1011
Text,
1112
VStack,
1213
} from "@chakra-ui/react";
@@ -67,18 +68,27 @@ const Login = (): React.ReactElement => {
6768
const { authenticatedUser, setAuthenticatedUser } = useContext(AuthContext);
6869
const [email, setEmail] = useState("");
6970
const [password, setPassword] = useState("");
70-
const [error, setError] = useState(false);
71+
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
72+
73+
const [login,
74+
{ loading: loginLoading, error: loginError },
75+
] = useMutation<{ login: LoginData }>(LOGIN);
7176

72-
const [login] = useMutation<{ login: LoginData }>(LOGIN);
7377

7478
const onLogInClick = async () => {
7579
let user: AuthenticatedUser | null = null;
7680
try {
7781
user = await authAPIClient.login(email, password, "", login);
78-
setError(false);
82+
setErrorMessage(undefined);
7983
} catch (e: unknown) {
80-
logPossibleGraphQLError(e as ApolloError, setAuthenticatedUser);
81-
setError(true);
84+
const errorCasted = e as ApolloError
85+
logPossibleGraphQLError(errorCasted, setAuthenticatedUser);
86+
if ((errorCasted?.message ?? "").indexOf("Failed to sign-in") !== -1) {
87+
setErrorMessage("Invalid email or password, please try again!")
88+
}
89+
else {
90+
setErrorMessage("An unexpected error occurred, please try again!")
91+
}
8292
}
8393
setAuthenticatedUser(user);
8494
};
@@ -116,14 +126,14 @@ const Login = (): React.ReactElement => {
116126
>
117127
Log in to account
118128
</Text>
119-
{error ? (
129+
{errorMessage ? (
120130
<Text
121131
pb={5}
122132
textAlign="center"
123133
variant={{ base: "mobile-caption", md: "desktop-caption" }}
124134
color="secondary.critical"
125135
>
126-
The email or password you entered is incorrect. Please try again.
136+
{errorMessage}
127137
</Text>
128138
) : (
129139
<Text
@@ -136,7 +146,7 @@ const Login = (): React.ReactElement => {
136146
)}
137147
<Flex width="100%" justifyContent="flexStart" flexDirection="column">
138148
<Box>
139-
<FormControl pb={5} isRequired isInvalid={error}>
149+
<FormControl pb={5} isRequired isInvalid={errorMessage !== undefined}>
140150
<FormLabel
141151
variant={{
142152
base: "mobile-form-label-bold",
@@ -154,7 +164,7 @@ const Login = (): React.ReactElement => {
154164
</FormControl>
155165
</Box>
156166
<Box>
157-
<FormControl pb={2} isRequired isInvalid={error}>
167+
<FormControl pb={2} isRequired isInvalid={errorMessage !== undefined}>
158168
<FormLabel
159169
variant={{
160170
base: "mobile-form-label-bold",
@@ -188,6 +198,7 @@ const Login = (): React.ReactElement => {
188198
pt={1}
189199
pb={1}
190200
backgroundColor="primary.blue"
201+
disabled={loginLoading}
191202
>
192203
<Text
193204
variant={{
@@ -196,7 +207,17 @@ const Login = (): React.ReactElement => {
196207
}}
197208
color="text.white"
198209
>
199-
Log in
210+
{loginLoading ? (
211+
<Spinner
212+
thickness="4px"
213+
speed="0.65s"
214+
emptyColor="gray.200"
215+
color="primary.green"
216+
size="lg"
217+
/>
218+
) : (
219+
"Login"
220+
)}
200221
</Text>
201222
</Button>
202223
<HStack>

0 commit comments

Comments
 (0)