Update login.html

This commit is contained in:
xfarrow 2024-02-28 12:08:00 +01:00
parent 6e3fd69bf8
commit edcd53e761

View File

@ -29,32 +29,38 @@
</div> </div>
<script> <script>
function login() { async function login() {
const email = document.getElementById("email").value; const email = document.getElementById("email").value;
const password = document.getElementById("password").value; const password = document.getElementById("password").value;
if(!email || !password){ if(!email || !password){
return; alert('Please fill in all fields');
return;
} }
const response = await fetch("http://localhost:3000/api/login", {
method: "POST",
body: JSON.stringify({
email: email,
password: password }),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
});
const options = { const data = await response.json();
method: 'POST', if(response.status != 200){
headers: { if(response.status == 401){
'Content-Type': 'application/json' alert('Login has failed');
}, }
body: JSON.stringify({ email, password }), else{
}; alert('Internal server error');
}
fetch('http://localhost:3000/blinkapi/login', options)
.then(response => {})
.then(data => {
document.cookie = `token=${data.token};`;
})
.catch(err => {
alert("An error has occurred :-( please try again later")
console.error(err);
});
} }
else{
alert("Login was successful. Token will be stored in a cookie");
document.cookie = `token=${data.token};`;
}
}
</script> </script>
</body> </body>