forked from RyanisyydsTT/skyport-tw-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.ejs
326 lines (284 loc) · 17.5 KB
/
account.ejs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<%- include('components/template') %>
<main id="content">
<div class="px-4 sm:px-6 lg:px-8">
<dl class="mt-5 grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-2">
<div class="p-3 bg-transparent shadow-xl border border-white/5 rounded-xl overflow-hidden sm:p-6 lg:p-10 card w-full">
<div class="lg:flex lg:items-center lg:justify-between">
<div class="min-w-0 flex-1">
<h2 class="text-md font-medium tracking-tight">
<span class="text-white flex flex-row"><%= req.translations.updateAccount %></span>
<span id="statusBg"
class="bg-neutral-600/20 hidden text-neutral-400 text-sm ml-1.5 font-medium me-2 px-2.5 py-1 rounded-full">
<span id="status"><%= user.username %></span>
</span>
</h2>
<form id="change-username-form" action="/change-username" method="POST" class="mt-5">
<div class="flex flex-col sm:flex-row justify-between">
<div class="w-full sm:w-1/2 sm:mr-4 mb-4 sm:mb-0">
<label class="text-neutral-400 text-sm tracking-tight mb-2"><%= req.translations.username %>:</label>
<input id="username" type="text" name="username"
class="rounded-xl focus:ring-transparent focus:border-transparent text-sm mt-1 mb-0 w-full items-center transition justify-left gap-16 hover:bg-white/5 px-4 py-2 flex border-white/5 bg-neutral-600/20 dark:bg-neutral-900 placeholder-neutral-400 text-white border border-black/10"
placeholder="<%= user.username %>" />
<span id="username-feedback" class="mt-2 border border-neutral-600/30 text-neutral-400 text-xs font-medium me-2 px-2.5 py-1 rounded-full"><%= req.translations.checking %></span>
</div>
<div class="w-full sm:w-1/2">
<label class="text-neutral-400 text-sm tracking-tight mt-4 sm:mt-0"><%= req.translations.email %>:</label>
<input id="email" type="email" name="email" value="" class="text-white rounded-xl focus:ring-transparent focus:border-transparent text-sm mt-1 mb-0 w-full items-center transition justify-left gap-16 hover:bg-white/5 px-4 py-2 flex border-white/5 bg-neutral-600/20 dark:bg-neutral-900 placeholder:text-white/20 border border-black/10" placeholder="<%= user.email %>" disabled />
</div>
</div>
<button type="submit" id="change-username-btn" class="mt-4 block rounded-xl bg-white px-3 py-2 text-center text-sm font-medium text-neutral-800 shadow-lg hover:bg-neutral-200 transition focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" disabled><%= req.translations.updateUsername %></button>
</form>
<form id="change-password-form" action="/change-password" method="POST" class="mt-5">
<label class="text-neutral-400 text-sm tracking-tight mb-3"><%= req.translations.currentPassword %>:</label>
<div id="current-password-feedback" class="border border-neutral-600/30 text-neutral-400 text-xs ml-1.5 font-medium me-2 px-2.5 py-1 rounded-full"><%= req.translations.checking %></div>
<input id="currentPassword" type="password" name="currentPassword" class="rounded-xl focus:ring-transparent focus:border-transparent text-sm mt-1 mb-0 w-full items-center transition justify-left gap-16 hover:bg-white/5 px-4 py-2 flex border-white/5 bg-neutral-600/20 dark:bg-neutral-900 placeholder-neutral-400 text-white border border-black/10" placeholder="<%= req.translations.currentPasswordHide %>" />
<label class="block text-neutral-400 text-sm tracking-tight mt-4"><%= req.translations.newPassword %>:</label>
<input id="newPassword" type="password" name="newPassword" class="rounded-xl focus:ring-transparent focus:border-transparent text-sm mt-1 mb-0 w-full items-center transition justify-left gap-16 hover:bg-white/5 px-4 py-2 flex border-white/5 bg-neutral-600/20 dark:bg-neutral-900 placeholder-neutral-400 text-white border border-black/10" placeholder="<%= req.translations.newPasswordHide %>" disabled />
<button type="submit" class="mt-4 block rounded-xl bg-white px-3 py-2 text-center text-sm font-medium text-neutral-800 shadow-lg hover:bg-neutral-200 transition focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"><%= req.translations.update_password %></button>
</form>
<% if (user.twoFAEnabled !== true) { %>
<button onclick="window.location.href = '/enable-2fa';" class="mt-4 block rounded-xl bg-white px-3 py-2 text-center text-sm font-medium text-neutral-800 shadow-lg hover:bg-neutral-200 transition focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"><%= req.translations.enabled2FA %></button>
<% } else { %>
<form action="/disable-2fa" method="POST">
<button class="mt-4 block rounded-xl bg-white px-3 py-2 text-center text-sm font-medium text-neutral-800 shadow-lg hover:bg-neutral-200 transition focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"><%= req.translations.disable2FA %></button>
</form>
<% } %>
</div>
</div>
</div>
</dl>
</div>
</main>
<style>
#username-feedback, #current-password-feedback {
transition: opacity 0.3s ease-in-out;
display: inline-block; /* Display as inline block */
min-width: fit-content; /* Set minimum width to fit the content */
max-width: 100%; /* Set maximum width to 100% of the parent container */
}
</style>
<script>
document.addEventListener('DOMContentLoaded', () => {
const currentPasswordInput = document.getElementById('currentPassword');
const newPasswordInput = document.getElementById('newPassword');
const currentPasswordFeedback = document.getElementById('current-password-feedback');
let currentPasswordValue = '';
let currentPasswordTimeoutId;
let controller;
let isLoading = true;
function showLoadingAnimation() {
currentPasswordFeedback.innerHTML = `
<span id="current-password-status">
<svg class="animate-spin h-3 w-3 inline-flex mr-1" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg> <span id="current-password-feedback-message"><%= req.translations.checking %></span>
</span>
`;
}
function clearPasswordFeedback() {
currentPasswordFeedback.innerHTML = ''; // Clear feedback message
currentPasswordFeedback.style.display = 'none'; // Hide the feedback div
newPasswordInput.disabled = true; // Disable new password input
newPasswordInput.value = ''; // Clear the new password input
}
// Call clearPasswordFeedback function to remove feedback for password when the page loads
clearPasswordFeedback();
currentPasswordInput.addEventListener('input', () => {
// Disable the new password input when the current password changes
newPasswordInput.disabled = true;
newPasswordInput.value = ''; // Clear the new password input
// Store the new current password value
currentPasswordValue = currentPasswordInput.value;
});
currentPasswordInput.addEventListener('input', async () => {
clearTimeout(currentPasswordTimeoutId);
if (!isLoading) {
showLoadingAnimation();
isLoading = true;
}
if (controller) {
controller.abort(); // Abort previous request if it exists
}
controller = new AbortController(); // Create a new AbortController
const signal = controller.signal; // Get the signal from the controller
currentPasswordTimeoutId = setTimeout(async () => {
const currentPassword = currentPasswordValue;
// Check if the current password is blank
if (currentPassword.trim() === '') {
clearPasswordFeedback();
isLoading = false; // Set isLoading to false if the input is blank
return;
}
try {
// Show loading animation
const response = await fetch('/validate-password', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ currentPassword }),
signal // Pass the signal to the request
});
if (response.ok) {
const data = await response.json();
if (data.valid) {
currentPasswordFeedback.innerHTML = `<span id="current-password-status"><%= req.translations.correctPassword %></span>`;
} else {
currentPasswordFeedback.innerHTML = `<span id="current-current-status"><%= req.translations.incorrectPassword %></span>`;
}
// Enable or disable the new password input based on validation result
newPasswordInput.disabled = !data.valid;
currentPasswordFeedback.style.display = 'inline-block'; // Show the feedback div
isLoading = false;
} else {
const errorMessage = await response.text();
currentPasswordFeedback.innerText = `<%= req.translations.updatingUsernameError %>`;
currentPasswordFeedback.style.color = 'red';
currentPasswordFeedback.style.display = 'inline-block'; // Show the feedback div
}
} catch (error) {
console.error('Error validating password:', error);
currentPasswordFeedback.innerText = `<%= req.translations.validadingpasswordError %>`;
currentPasswordFeedback.style.color = 'red';
currentPasswordFeedback.style.display = 'inline-block'; // Show the feedback div
}
}, 500); // Adjust delay time as needed
});
// Trigger initial check when the page is loaded
if (currentPasswordInput.value.trim() !== '') {
checkCurrentPassword();
} else {
isLoading = false; // Set isLoading to false if the input is blank initially
}
});
</script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const currentPasswordInput = document.getElementById('currentPassword');
const newPasswordInput = document.getElementById('newPassword');
const changePasswordBtn = document.querySelector('#change-password-form button[type="submit"]');
// Function to check if the "Change Password" button should be enabled or disabled
function toggleChangePasswordButton() {
if (currentPasswordInput.value.trim() !== '' && newPasswordInput.value.trim() !== '') {
changePasswordBtn.disabled = false;
} else {
changePasswordBtn.disabled = true;
}
}
// Event listener for input on current password field
currentPasswordInput.addEventListener('input', () => {
toggleChangePasswordButton();
});
// Event listener for input on new password field
newPasswordInput.addEventListener('input', () => {
toggleChangePasswordButton();
});
// Trigger initial check when the page is loaded
toggleChangePasswordButton();
});
</script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const usernameInput = document.getElementById('username');
const changeUsernameBtn = document.getElementById('change-username-btn');
const usernameFeedback = document.getElementById('username-feedback');
let isLoading = false;
// Function to display loading animation
function showLoadingAnimation() {
usernameFeedback.innerHTML = `
<span id="username-status">
<svg class="animate-spin h-3 w-3 inline-flex mr-1" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg> <span id="username-feedback-message"><%= req.translations.checking %></span>
</span>
`;
}
// Function to check if a username exists
let timeoutId; // Variable to store the timeout ID for debouncing
async function checkUsername() {
const username = usernameInput.value.trim().toLowerCase(); // Convert input to lowercase
// Hide feedback elements if username is empty
if (username === '') {
usernameFeedback.style.display = 'none';
changeUsernameBtn.disabled = true;
return;
} else {
usernameFeedback.style.display = 'inline-block';
}
// Clear previous timeout to prevent multiple requests
clearTimeout(timeoutId);
// Show loading animation if not already loading
if (!isLoading) {
showLoadingAnimation();
isLoading = true;
}
// Set timeout to wait for user to finish typing
timeoutId = setTimeout(async () => {
// Check if username already exists
const response = await fetch(`/check-username?username=${encodeURIComponent(username)}`);
const data = await response.json();
// Update UI based on response
if (data.exists) {
usernameFeedback.innerHTML = `<span id="username-status"><%= req.translations.usernameTaken %></span>`;
changeUsernameBtn.disabled = true;
} else {
usernameFeedback.innerHTML = `<span id="username-status"><%= req.translations.usernameIsNotTaken %></span>`;
changeUsernameBtn.disabled = false;
}
isLoading = false;
}, 500); // Adjust delay time as needed
}
// Event listener for input on username field with debouncing
usernameInput.addEventListener('input', () => {
clearTimeout(timeoutId); // Clear previous timeout
timeoutId = setTimeout(checkUsername, 500); // Set new timeout with debouncing delay
});
// Event listener for form submission to prevent default behavior
document.getElementById('change-username-form').addEventListener('submit', (event) => {
event.preventDefault();
});
// Clear feedback elements and disable button when username input is cleared
usernameInput.addEventListener('change', () => {
if (usernameInput.value.trim() === '') {
usernameFeedback.style.display = 'none';
usernameFeedbackMessage.innerText = '';
changeUsernameBtn.disabled = true;
}
});
// Trigger initial check when the page is loaded
checkUsername();
async function handleUsernameUpdate(event) {
event.preventDefault();
const currentUsername = '<%= user.username %>'; // Get current username from server-side template
const newUsername = usernameInput.value.trim(); // Get new username from input field
try {
// Send a POST request to the server to update the username
const response = await fetch('/update-username', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ currentUsername, newUsername })
});
if (response.ok) {
// Username updated successfully
alert('<%= req.translations.usernameUpdatedSuccess %>');
window.location.href = '/login?err=UpdatedCredentials';
// Optionally, you can redirect the user to another page or update the UI
} else {
// Error updating username
const errorMessage = await response.text();
alert(errorMessage);
}
} catch (error) {
console.error('Error updating username:', error);
alert('<%= req.translations.updatingUsernameError %>');
}
}
// Add event listener to the form for submit event
document.getElementById('change-username-form').addEventListener('submit', handleUsernameUpdate);
});
</script>
<%- include('components/footer') %>