Update page.tsx
This commit is contained in:
@@ -48,21 +48,16 @@ export default function LoginPage() {
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (response.ok && data.token) {
|
if (response.ok && data.token) {
|
||||||
// Set redirecting state to prevent multiple redirect attempts
|
|
||||||
setIsRedirecting(true);
|
setIsRedirecting(true);
|
||||||
|
|
||||||
// Store the token in both cookie and localStorage for redundancy
|
|
||||||
document.cookie = `Authorization=${data.token}; path=/; Secure; SameSite=Strict; max-age=604800`;
|
document.cookie = `Authorization=${data.token}; path=/; Secure; SameSite=Strict; max-age=604800`;
|
||||||
localStorage.setItem("Authorization", data.token);
|
localStorage.setItem("Authorization", data.token);
|
||||||
|
|
||||||
// Show toast before redirect
|
|
||||||
toast.success("Login successful");
|
toast.success("Login successful");
|
||||||
|
|
||||||
// Use a small timeout to ensure the cookie is set before navigation
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
try {
|
try {
|
||||||
router.push("/dashboard");
|
router.push("/dashboard");
|
||||||
// Force a hard redirect if router.push doesn't work
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = "/dashboard";
|
window.location.href = "/dashboard";
|
||||||
}, 1000);
|
}, 1000);
|
||||||
@@ -72,14 +67,12 @@ export default function LoginPage() {
|
|||||||
}
|
}
|
||||||
}, 300);
|
}, 300);
|
||||||
} else {
|
} else {
|
||||||
// Handle HTTP error responses (including 401)
|
|
||||||
const errorMessage = data.error || "Invalid credentials";
|
const errorMessage = data.error || "Invalid credentials";
|
||||||
toast.error("Login Failed", {
|
toast.error("Login Failed", {
|
||||||
description: errorMessage,
|
description: errorMessage,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// This will now only catch network errors or JSON parsing errors
|
|
||||||
toast.error("Connection Error", {
|
toast.error("Connection Error", {
|
||||||
description: "Unable to connect to the server. Please check your internet connection and try again.",
|
description: "Unable to connect to the server. Please check your internet connection and try again.",
|
||||||
});
|
});
|
||||||
@@ -89,7 +82,6 @@ export default function LoginPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If already redirecting, show loading state
|
|
||||||
if (isRedirecting) {
|
if (isRedirecting) {
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center justify-center min-h-screen bg-gray-100 dark:bg-[#0F0F12]">
|
<div className="flex items-center justify-center min-h-screen bg-gray-100 dark:bg-[#0F0F12]">
|
||||||
|
|||||||
Reference in New Issue
Block a user