64 lines
2.3 KiB
JavaScript
64 lines
2.3 KiB
JavaScript
document.addEventListener('DOMContentLoaded', async function () {
|
|
const backToLoginButton = document.getElementById('backToLogin');
|
|
const resetPasswordButton = document.getElementById('resetPassword');
|
|
|
|
// Retrieve the operation codes via IPC
|
|
const operationCodes = await window.electronAPI.getOperationsCodes();
|
|
if (!operationCodes) {
|
|
await window.electronAPI.showAlert('Internal error of the application.');
|
|
return;
|
|
}
|
|
|
|
const codeResetPassword = operationCodes.RESET_PASSWORD;
|
|
const codeOk = operationCodes.OK;
|
|
|
|
// Back to login
|
|
backToLoginButton.addEventListener('click', function (e) {
|
|
e.preventDefault();
|
|
window.electronAPI.changeContent('login');
|
|
});
|
|
|
|
// Reset password logic
|
|
resetPasswordButton.addEventListener('click', async function (e) {
|
|
e.preventDefault();
|
|
if (!await window.electronAPI.openUcSocket()) {
|
|
await window.electronAPI.showAlert('Internal error of the application.');
|
|
return;
|
|
}
|
|
|
|
const form = document.getElementById('resetPasswordForm');
|
|
const formData = new FormData(form);
|
|
const email = formData.get('email');
|
|
const newPassword = formData.get('newPassword');
|
|
|
|
// Ensure the email and new password are provided
|
|
if (!email || !newPassword) {
|
|
await window.electronAPI.showAlert('Please provide both email and new password.');
|
|
return;
|
|
}
|
|
|
|
if (!await attemptResetPassword(email, newPassword)) {
|
|
return;
|
|
}
|
|
|
|
await window.electronAPI.closeUcSocket();
|
|
await window.electronAPI.showAlert('Password reset successfully.');
|
|
window.electronAPI.changeContent('login');
|
|
});
|
|
|
|
async function attemptResetPassword(email, newPassword) {
|
|
const app_type = await window.electronAPI.readUserConfig('app_type');
|
|
const messageData = { email, newPassword, app_type };
|
|
|
|
if (!await window.electronAPI.sendUcMessage(codeResetPassword, messageData)) return false;
|
|
|
|
const response = await waitForResponse();
|
|
if (response && response.operationCode === codeOk) {
|
|
return true;
|
|
} else {
|
|
await window.electronAPI.showAlert(response?.metaInfo?.message || 'Error resetting password.');
|
|
return false;
|
|
}
|
|
}
|
|
});
|