-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatepassword.html
124 lines (110 loc) · 4.08 KB
/
updatepassword.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Change Password</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav class="navbar">
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="adminhome.html">Home</a>
<a href="createaccount.html">Create User Account</a>
<a href="manageaccount.html">User Account Management</a>
<a href="itemsales.html">Item Sales Performance</a>
<a href="salesperformance.html">Salesman Sales Performance</a>
</div>
<span class="open" onclick="openNav()">  ☰  
<div class="logo">
<a href="">
<img src="assets/sale-sign.png" alt="SDS_logo">
<span>  SALES PILOT  </span>
<img src="assets/cashier-machine.png" alt="SDS_logo">
</a>
</div>
</span>
<a class="logout" href="#" onclick="logout()">Log Out  </a>
</nav>
<form id="changePasswordForm">
<table class="updatePass">
<caption>Change Password</caption>
<tr>
<td><label for="email">Email:</label></td>
<td><input type="email" id="email" name="email" required></td>
</tr>
<tr>
<td><label for="current_password">Current Password:</label></td>
<td><input type="password" id="current_password" name="current_password" required></td>
</tr>
<tr>
<td><label for="new_password">New Password:</label></td>
<td><input type="password" id="new_password" name="new_password" required></td>
</tr>
<tr>
<td><label for="confirm_password">Confirm Password:</label></td>
<td><input type="password" id="confirm_password" name="confirm_password" required></td>
</tr>
<tr>
<td></td>
<td><button type="submit">Change Password</button><br></td>
</tr>
</table>
</form>
<div id="result"></div>
<script>
// Open Side Navigation Bar
function openNav() {
document.getElementById("mySidenav").style.width = "280px";
}
// Close Side Navigation Bar
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
// Logout and clear data from local storage
function logout() {
localStorage.removeItem("token");
window.location.href = "login.html";
}
document.getElementById('changePasswordForm').addEventListener('submit', changePassword);
async function changePassword(event) {
event.preventDefault();
const form = event.target;
const email = form.elements.email.value;
const current_password = form.elements.current_password.value;
const new_password = form.elements.new_password.value;
const confirm_password = form.elements.confirm_password.value;
if (new_password !== confirm_password) {
alert("Error: New password and confirmed password do not match.");
return;
}
const payload = { email, current_password, new_password, confirmed_password: confirm_password };
try {
const token = localStorage.getItem("token");
const response = await fetch('http://localhost:3000/api/account/change/pass', {
method: 'PUT',
headers: {
"Authorization": `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (response.ok) {
const responseData = await response.json();
alert("Password changed successfully");
location.reload();
} else {
const errorData = await response.json();
const errorMessage = errorData.error;
alert(`Error changing password: ${errorMessage}`);
location.reload();
}
} catch (error) {
console.error(error);
alert('An error occurred while changing the password.');
location.href();
}
}
</script>
</body>
</html>