Files
inventory/public/index.html
2023-11-23 16:01:56 +01:00

171 lines
5.3 KiB
HTML

<!-- FILEPATH: /home/emerald/Documents/IPR/Inventory/public/index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
.container {
text-align: center;
}
.menu {
margin-top: 20px;
}
/* CSS styles for inputs */
input[type="text"],
input[type="password"] {
padding: 10px;
margin-bottom: 10px;
width: 200px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button[type="submit"]:hover {
background-color: #45a049;
}
/* CSS styles for the selection menu */
.selection-menu {
display: none;
}
.selection-menu h2 {
margin-top: 20px;
}
.selection-menu button {
margin: 10px;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.selection-menu button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<form id="loginForm">
<h1>Login</h1>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">Login</button>
</form>
<div id="menu" class="menu selection-menu">
<h2>Menu</h2>
<button onclick="redirectTo('computers')">Computers</button>
<button onclick="redirectTo('tablets')">Tablets</button>
<button onclick="redirectTo('projectors')">Projectors</button>
</div>
</div>
<script>
const loginForm = document.getElementById('loginForm');
const menu = document.getElementById('menu');
loginForm.addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
// Make a POST request to the login endpoint
fetch('http://localhost:3000/user/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
})
.then(response => response.json())
.then(data => {
// Check if login is valid
if (data.token) {
localStorage.setItem('token', data.token);
loginForm.style.display = 'none'; // Hide the login form
menu.style.display = 'block';
} else {
alert('Invalid login');
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred');
});
});
function redirectTo(page) {
// Redirect to the proper page based on the user's choice
window.location.href = `/${page.charAt(0)}`;
}
// check if user is logged in
window.onload = checkIfLoggedIn();
async function checkIfLoggedIn() {
const token = localStorage.getItem('token');
if (token) {
loginForm.style.display = 'none'; // Hide the login form
menu.style.display = 'block';
const valid = await checkIfTokenIsValid(token);
if (!valid) {
localStorage.removeItem('token');
loginForm.style.display = 'block'; // Show the login form
menu.style.display = 'none';
}
}
}
async function checkIfTokenIsValid(token) {
// Make a POST request to the login endpoint
return await fetch('http://localhost:3000/user/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ token })
})
.then(response => {
if (response.status === 200) {
return true;
} else {
return false;
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred');
});
}
</script>
</body>
</html>