-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
42 lines (35 loc) · 1.41 KB
/
auth.js
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
let authCredentials = { username: '', password: '' };
async function authenticateUser(username, password) {
const response = await fetch('auth.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
const result = await response.json();
return result.authenticated;
}
function checkAuthentication() {
if (authCredentials.username && authCredentials.password) {
document.getElementById('loginForm').style.display = 'none';
document.getElementById('uploadForm').style.display = 'block';
loadFileList();
} else {
document.getElementById('loginForm').style.display = 'block';
document.getElementById('uploadForm').style.display = 'none';
}
}
document.getElementById('authForm').addEventListener('submit', async function(event) {
event.preventDefault();
const username = document.getElementById('loginUsername').value;
const password = document.getElementById('loginPassword').value;
const isAuthenticated = await authenticateUser(username, password);
if (isAuthenticated) {
authCredentials = { username, password };
checkAuthentication();
} else {
document.getElementById('statusMessage').innerHTML = 'Incorrect username or password!';
}
});
document.addEventListener('DOMContentLoaded', checkAuthentication);