-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
57 lines (50 loc) · 2.24 KB
/
script.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const adminCredentials = {
username: 'root@1234',
password: 'admin@1234'
};
const customerCredentials = {
username: 'tester@123',
password: 'synchrony'
};
document.getElementById('login-form').addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
if (username === adminCredentials.username && password === adminCredentials.password) {
window.location.href = 'dashboard.html';
sessionStorage.setItem('userType', 'admin');
} else if (username === customerCredentials.username && password === customerCredentials.password) {
window.location.href = 'dash_index.html';
sessionStorage.setItem('userType', 'customer');
} else {
const encryptedPassword = await encryptPassword(password);
validateCustomerLogin(username, encryptedPassword);
}
});
function validateCustomerLogin(username, encryptedPassword) {
if (username === customerCredentials.username && encryptedPassword === 'mockEncryptedPassword') {
window.location.href = 'dash_index.html';
sessionStorage.setItem('userType', 'customer');
} else {
document.getElementById('login-error').textContent = 'Incorrect username or password';
}
}
async function encryptPassword(password) {
const encoder = new TextEncoder();
const data = encoder.encode(password);
const hash = await crypto.subtle.digest('SHA-256', data);
return hex(hash);
}
function hex(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
window.addEventListener('DOMContentLoaded', () => {
const userType = sessionStorage.getItem('userType');
if (userType === 'admin') {
document.getElementById('dashboard-heading').textContent = 'Admin Dashboard';
document.getElementById('dashboard-message').textContent = 'Welcome, admin!';
} else if (userType === 'customer') {
document.getElementById('dashboard-heading').textContent = 'Customer Dashboard';
document.getElementById('dashboard-message').textContent = 'Welcome, customer!';
}
});