Skip to content

Commit

Permalink
Refactor login.js to handle network errors and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles-Lucien committed Jan 4, 2024
1 parent e7dd947 commit 6c9e84b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions FrontEnd/scripts/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ function loginUser() {
password: password,
}),
})
.then((response) => response.json())
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error("Network response was not ok.");
}
})
.then((data) => {
const token = data.token;
// Store the token in localStorage
localStorage.setItem("token", token);
// Redirect to index.html if token is defined
token && (window.location.href = "./index.html");
// If token is undefined, alert the user
token || alert("Invalid email or password");

// vérifier que response.ok est true

})
.catch((error) => {
console.error("Error:", error);
alert("Invalid email or password");
});
}

Expand Down

0 comments on commit 6c9e84b

Please sign in to comment.