update front-end

This commit is contained in:
xfarrow 2024-02-28 15:13:19 +01:00
parent edcd53e761
commit e62631b42f
5 changed files with 11 additions and 13 deletions

View File

@ -88,7 +88,7 @@ async function login (req, res) {
const token = jwtUtils.generateToken(person.id);
return res.status(200).json({ token });
} else {
return res.status(401).json({ error: 'Unauthorized' });
return res.status(401).json({ error: 'Invalid credentials' });
}
} catch (error) {
console.error(`Error in function ${login.name}: ${error}`);

View File

@ -1 +0,0 @@
const apiUrl = 'http://localhost:3000/blinkapi';

View File

@ -2,7 +2,7 @@
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>HTML5 Login Form with validation Example</title>
<title>Log in - Blink</title>
<link rel="stylesheet" href="../css/login-register.css">
</head>
<body>
@ -28,6 +28,7 @@
</div>
</div>
<script src="../js/constants.js"></script>
<script>
async function login() {
const email = document.getElementById("email").value;
@ -37,7 +38,7 @@
alert('Please fill in all fields');
return;
}
const response = await fetch("http://localhost:3000/api/login", {
const response = await fetch(`${API_URL}/login`, {
method: "POST",
body: JSON.stringify({
email: email,
@ -48,17 +49,14 @@
});
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");
if(response.ok){
console.log(`Login was successful. Token is ${data.token}`);
document.cookie = `token=${data.token};`;
window.location.href = 'userprofile.html?id=myself';
}
else{
alert(data.error);
}
}
</script>

View File

@ -0,0 +1 @@
const API_URL = 'http://localhost:3000/api';