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