-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
68 lines (66 loc) · 2.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<style>
body {
background-color: #f8f9fa;
}
</style>
<body class="bg-light d-flex justify-content-center align-items-center min-vh-100">
<div class="card shadow-sm">
<div class="card-body">
<h3 class="card-title text-center mb-4">Login</h3>
<form id="loginForm">
<div class="mb-3">
<label for="login" class="form-label visually-hidden">Login</label>
<input type="text" class="form-control" name="login" id="login" placeholder="Login" required />
</div>
<div class="mb-3">
<label for="password" class="form-label visually-hidden">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" required />
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
const BASE_URL = "http://localhost:3003/api/users/login";
document
.getElementById("loginForm")
.addEventListener("submit", async function (event) {
event.preventDefault();
const login = document.getElementById("login").value;
const password = document.getElementById("password").value;
try {
const response = await fetch(BASE_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ login, password }),
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
localStorage.setItem("token", data.token);
window.location.href = "public/dashboard.html";
} catch (error) {
Swal.fire({
icon: "error",
title: "Login Error",
text: "Invalid username or password. Please try again.",
});
console.error("Error:", error);
}
});
</script>
</body>
</html>