From 907ead75e1f004fe34ee2ad6dd6df7aa6e65aeed Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Thu, 27 Feb 2025 20:51:25 +0000 Subject: [PATCH] Update page.tsx --- app/auth/login/page.tsx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/auth/login/page.tsx b/app/auth/login/page.tsx index e661160..e93cb44 100644 --- a/app/auth/login/page.tsx +++ b/app/auth/login/page.tsx @@ -22,28 +22,28 @@ export default function LoginPage() { setIsLoading(true); try { - const res = await fetchData(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, { + const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }), }); - - if (res.token) { - document.cookie = `Authorization=${res.token}; path=/; Secure; SameSite=Strict; max-age=604800`; + + const data = await response.json(); + + if (response.ok && data.token) { + document.cookie = `Authorization=${data.token}; path=/; Secure; SameSite=Strict; max-age=604800`; router.push("/dashboard"); } else { - const data = await res.json(); + // Handle HTTP error responses (including 401) const errorMessage = data.error || "Invalid credentials"; - - // Show toast notification for failed login toast.error("Login Failed", { description: errorMessage, }); } } catch (error) { - // Handle network errors or other exceptions - toast.error("Login Error", { - description: "An error occurred while trying to log in. Please try again.", + // This will now only catch network errors or JSON parsing errors + toast.error("Connection Error", { + description: "Unable to connect to the server. Please check your internet connection and try again.", }); } finally { setIsLoading(false);