Skip to content

Commit

Permalink
Merge pull request #176 from CS3219-AY2425S1/fix/bug/login-fail-toast
Browse files Browse the repository at this point in the history
Fix login failed toast vague message
  • Loading branch information
SelwynAng authored Oct 20, 2024
2 parents bfdd8ec + d5b092f commit c13d229
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
11 changes: 10 additions & 1 deletion frontend/app/auth/auth-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
);

if (!response.ok) {
throw new Error("Not OK");
switch (response.status) {
case 400:
throw new Error("Email and/or password is missing.");
case 401:
throw new Error("Invalid email or password.");
case 500:
throw new Error("Internal server error. Please try again later.");
default:
throw new Error("Unexpected error occurred.");
}
}

const resJson = await response.json();
Expand Down
37 changes: 31 additions & 6 deletions frontend/components/auth/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,37 @@ export function LoginForm() {
description: "Login Failed.",
});
}
} catch (err) {
toast({
title: "Error",
variant: "destructive",
description: "Login Failed.",
});
} catch (err: unknown) {
if (err instanceof Error) {
let description_text = "";
switch (err.message) {
case "Email and/or password is missing.":
description_text = "Please provide both email and password.";
break;
case "Invalid email or password.":
description_text = "Username or password is incorrect.";
break;
case "Internal server error. Please try again later.":
description_text =
"There was an issue with the server. Please try again later.";
break;
default:
description_text =
"An unexpected error occurred. Please try again.";
break;
}
toast({
title: "Error",
variant: "destructive",
description: description_text,
});
} else {
toast({
title: "Error",
variant: "destructive",
description: "An unexpected error occurred. Please try again.",
});
}
}
};

Expand Down

0 comments on commit c13d229

Please sign in to comment.