blink/frontend/vanilla/html/login.html

67 lines
1.9 KiB
HTML
Raw Normal View History

2023-09-27 12:18:29 +02:00
<!DOCTYPE html>
<html lang="en" >
2023-10-18 15:36:43 +02:00
<head>
<meta charset="UTF-8">
<title>HTML5 Login Form with validation Example</title>
<link rel="stylesheet" href="../css/login-register.css">
</head>
<body>
<!-- partial:index.partial.html -->
<div id="login-form-wrap">
<h2>Login</h2>
<form id="login-form" method="POST">
<p>
<input type="email" id="email" name="email" placeholder="Email Address" required><i class="validation"><span></span><span></span></i>
</p>
<p>
<input type="password" id="password" name="password" placeholder="Password" required><i class="validation"><span></span><span></span></i>
</p>
<p>
<button type="button" onclick="login()">Login</button>
</p>
</form>
<div id="create-account-wrap">
<p>Not a member? <a href="./register.html">Create Account</a><p>
</div>
</div>
<script>
2024-02-28 12:08:00 +01:00
async function login() {
2023-10-18 15:36:43 +02:00
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
if(!email || !password){
2024-02-28 12:08:00 +01:00
alert('Please fill in all fields');
return;
2023-10-18 15:36:43 +02:00
}
2024-02-28 12:08:00 +01:00
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"
}
});
2023-10-18 15:36:43 +02:00
2024-02-28 12:08:00 +01:00
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};`;
2023-10-18 15:36:43 +02:00
}
2024-02-28 12:08:00 +01:00
}
2023-10-18 15:36:43 +02:00
</script>
</body>
2024-02-28 12:08:00 +01:00
</html>