Update login.html

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

View File

@ -29,31 +29,37 @@
</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){
alert('Please fill in all fields');
return; return;
} }
const response = await fetch("http://localhost:3000/api/login", {
const options = { method: "POST",
method: 'POST', body: JSON.stringify({
email: email,
password: password }),
headers: { headers: {
'Content-Type': 'application/json' "Content-type": "application/json; charset=UTF-8"
}, }
body: JSON.stringify({ email, password }),
};
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);
}); });
const data = await response.json();
if(response.status != 200){
if(response.status == 401){
alert('Login has failed');
}
else{
alert('Internal server error');
}
}
else{
alert("Login was successful. Token will be stored in a cookie");
document.cookie = `token=${data.token};`;
}
} }
</script> </script>