I'm creating a blog with a friend as a small project between us. I'm and tested the backend in PHP, he's writing the frontend code. As he is still newer than me HTML and JS, I'm also writing the JS code to handle comms with the backend. The issue we're facing and can't get our heads around is the following: Logging in is possible with username or email and the password. The login code works when
What doesn't work is the Email - password with vanilla JS fetch. In this case we get the TypeError: Load failed or Failed to fetch. This is the JS code:
const credential = document.getElementById('email').value;
const password = document.getElementById('password').value;
const body = JSON.stringify({ credential, password });
fetch('/api/auth/login', { method: 'POST', body: body, headers: { 'Content-Type': 'application/json; charset=UTF-8' } })
.then((res) => res.json())
.then((data) => {
if (data.access_token) {
localStorage.setItem('fit_token', data.access_token);
} else {
alert(data.message);
}
})
.catch((err) => console.log(err));
};
The catch isn't even reached, nothing gets logged in console. I only see the error when I debug...
Can anyone point us in the right direction?