-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.html
189 lines (165 loc) · 5.21 KB
/
admin.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!-- admin.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Admin Login</title>
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"
rel="stylesheet"
/>
<style>
body {
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f4f4f4;
}
.admin-login-container {
max-width: 400px;
width: 90%; /* Adjust the width as needed */
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
color: #333;
}
form {
display: flex;
flex-direction: column;
margin-top: 20px;
}
label {
margin-bottom: 8px;
color: #333;
}
input {
padding: 10px;
margin-bottom: 16px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
button {
padding: 12px;
background-color: #333;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
button:hover {
background-color: #555;
}
/* Media queries for responsiveness */
@media screen and (max-width: 768px) {
.admin-login-container {
max-width: 100%;
}
}
@media screen and (max-width: 480px) {
.admin-login-container {
width: 100%;
}
}
</style>
</head>
<body>
<div class="admin-login-container">
<h1>Admin Login</h1>
<form id="loginForm" onsubmit="submitForm(event)">
<label for="username">Username</label>
<input type="text" id="username" name="username" required />
<label for="password">Password</label>
<input type="password" id="password" name="password" required />
<button type="submit">Login</button>
</form>
</div>
<script src="config.js"></script>
<script>
const backendUrl = window.config.backendUrl;
const frontendUrl = window.config.frontendUrl;
async function checkAuthentication() {
const token = localStorage.getItem('token');
if (token && window.location.pathname === '/admin.html') {
// Token exists and on the login page, check token validity
try {
const response = await fetch(`${backendUrl}/check-auth`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
credentials: 'include',
});
if (response.ok) {
// Token is valid, redirect to dashboard
window.location.href = `${frontendUrl}/dashboard.html`;
} else {
// Token is invalid, stay on the login page
// console.log('Authentication failed. Redirecting to login page');
// console.log('Response status:', response.status);
// console.log('Response text:', await response.text());
localStorage.removeItem('token');
}
} catch (error) {
console.error('Error checking authentication:', error);
localStorage.removeItem('token');
}
} else if (
!localStorage.getItem('token') &&
window.location.pathname !== '/admin.html'
) {
localStorage.removeItem('token');
// No token and not on the login page, redirect to login
window.location.href = `${frontendUrl}/admin.html`;
}
}
// Call the checkAuthentication function when the page loads
checkAuthentication();
async function submitForm(event) {
event.preventDefault();
const form = document.getElementById('loginForm');
const formData = new FormData(form);
const jsonData = {};
formData.forEach((value, key) => {
jsonData[key] = value;
});
try {
const response = await fetch(`${backendUrl}/login`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(jsonData),
});
const data = await response.json();
if (response.ok) {
// Authentication successful, store the token
const token = data.token;
localStorage.setItem('token', token);
// Redirect to the dashboard
window.location.href = `${frontendUrl}/dashboard.html`;
} else {
// Authentication failed, handle error
alert('Invalid email or password');
console.error('Authentication failed:', data.message);
}
} catch (error) {
alert('Invalid email or password');
console.error('Error during login:', error);
}
}
</script>
</body>
</html>