Skip to content

Commit c13d229

Browse files
authored
Merge pull request #176 from CS3219-AY2425S1/fix/bug/login-fail-toast
Fix login failed toast vague message
2 parents bfdd8ec + d5b092f commit c13d229

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

frontend/app/auth/auth-context.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
7575
);
7676

7777
if (!response.ok) {
78-
throw new Error("Not OK");
78+
switch (response.status) {
79+
case 400:
80+
throw new Error("Email and/or password is missing.");
81+
case 401:
82+
throw new Error("Invalid email or password.");
83+
case 500:
84+
throw new Error("Internal server error. Please try again later.");
85+
default:
86+
throw new Error("Unexpected error occurred.");
87+
}
7988
}
8089

8190
const resJson = await response.json();

frontend/components/auth/login-form.tsx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,37 @@ export function LoginForm() {
4141
description: "Login Failed.",
4242
});
4343
}
44-
} catch (err) {
45-
toast({
46-
title: "Error",
47-
variant: "destructive",
48-
description: "Login Failed.",
49-
});
44+
} catch (err: unknown) {
45+
if (err instanceof Error) {
46+
let description_text = "";
47+
switch (err.message) {
48+
case "Email and/or password is missing.":
49+
description_text = "Please provide both email and password.";
50+
break;
51+
case "Invalid email or password.":
52+
description_text = "Username or password is incorrect.";
53+
break;
54+
case "Internal server error. Please try again later.":
55+
description_text =
56+
"There was an issue with the server. Please try again later.";
57+
break;
58+
default:
59+
description_text =
60+
"An unexpected error occurred. Please try again.";
61+
break;
62+
}
63+
toast({
64+
title: "Error",
65+
variant: "destructive",
66+
description: description_text,
67+
});
68+
} else {
69+
toast({
70+
title: "Error",
71+
variant: "destructive",
72+
description: "An unexpected error occurred. Please try again.",
73+
});
74+
}
5075
}
5176
};
5277

0 commit comments

Comments
 (0)