Update page.tsx

This commit is contained in:
NotII
2025-02-27 20:51:25 +00:00
parent 64853f9ff6
commit 907ead75e1

View File

@@ -22,28 +22,28 @@ export default function LoginPage() {
setIsLoading(true); setIsLoading(true);
try { 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", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }), body: JSON.stringify({ username, password }),
}); });
if (res.token) { const data = await response.json();
document.cookie = `Authorization=${res.token}; path=/; Secure; SameSite=Strict; max-age=604800`;
if (response.ok && data.token) {
document.cookie = `Authorization=${data.token}; path=/; Secure; SameSite=Strict; max-age=604800`;
router.push("/dashboard"); router.push("/dashboard");
} else { } else {
const data = await res.json(); // Handle HTTP error responses (including 401)
const errorMessage = data.error || "Invalid credentials"; const errorMessage = data.error || "Invalid credentials";
// Show toast notification for failed login
toast.error("Login Failed", { toast.error("Login Failed", {
description: errorMessage, description: errorMessage,
}); });
} }
} catch (error) { } catch (error) {
// Handle network errors or other exceptions // This will now only catch network errors or JSON parsing errors
toast.error("Login Error", { toast.error("Connection Error", {
description: "An error occurred while trying to log in. Please try again.", description: "Unable to connect to the server. Please check your internet connection and try again.",
}); });
} finally { } finally {
setIsLoading(false); setIsLoading(false);