I use javascript to connect to a login. I put console.log on the password and the email and apparently it recovers the password well but not the email and I don’t see where the problem comes from. I checked my email Id in the HTML and javascript code and nothing. Can you help me ? And tell me what’s wrong with my code. Thank you and good day
I changed getElementById by querySelector but nothing. My email Id is well written however everywhere. Here is the HTML
<form class="form" action="#" method="post">
<h3>Log In</h3>
<div class="input">
<label for="email">Email</label>
<input type="email" placeholder="Email" id="email">
<label for="password">Mot de passe</label>
<input type="password" placeholder="Mot de passe" id="password">
<input type="submit" value="Se connecter" id="submit">
<p id="errorMessage"></p>
<a class="link" href="#" >Mot de passe oublié</a>
<p id="erreur"></p>
</div>
</form>
Here is the javascript code
const submit = document.getElementById("submit");
const errorMessage = document.getElementById("errorMessage");
submit.addEventListener("click", (e) => {
e.preventDefault();
const email = document.querySelector("#email").value;
const password = document.querySelector("#password").value;
console.log("email: ", email);
console.log("password: ", password);
if (!email || !password) {
document.getElementById("errorMessage").innerHTML = "Veuillez remplir tous les champs";
return;
}
fetch("http://localhost:5678/api/users/login", {
method: "POST",
headers: {
accept: "application/json",
"Content-type": "application/json",
},
body: JSON.stringify({email: email, password: password}),
})
.then(authResponse => {
console.log("authResponse: ", authResponse);
if (authResponse.status === 200) {
return authResponse.json();
}
else if (authResponse.status === 401) {
errorMessage.textcontent = "Accès non autrorisé";
}
else if (authResponse.status === 404) {
errorMessage.textcontent = "Utilisateur non trouvé";
} else {
errorMessage.textcontent = `Error: ${authResponse.status}`;
}
})
.then(userData => {
console.log("userData: ", userData);
if (userData) {
window.localStorage.setItem("userData", JSON.stringify(userData));
window.location.replace = "admin.html";
}
})
.catch(error => console.error(error));
});
and the screeenshot of the console