Update page.tsx
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user