Files
FACULTATE-LICENTA/User/render/js/reset_password.js
T

67 lines
2.2 KiB
JavaScript

let codeResetPassword = '';
let codeOk = '';
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.networkAPI.getOperationsCodes();
if (!operationCodes) {
await window.uiAPI.showAlert('Internal error of the application.');
return;
}
codeResetPassword = operationCodes.RESET_PASSWORD;
codeOk = operationCodes.OK;
// Back to login
backToLoginButton.addEventListener('click', function (e) {
e.preventDefault();
window.uiAPI.changeContent('login');
});
// Reset password logic
resetPasswordButton.addEventListener('click', async function (e) {
e.preventDefault();
if (!await window.networkAPI.openUcSocket()) {
await window.uiAPI.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.uiAPI.showAlert('Please provide both email and new password.');
return;
}
if (!await attemptResetPassword(email, newPassword)) {
return;
}
await window.networkAPI.closeUcSocket();
await window.uiAPI.showAlert('Password reset successfully.');
await window.uiAPI.changeContent('login');
});
});
async function attemptResetPassword(email, newPassword) {
const app_type = await window.databaseAPI.getAppType();
const messageData = { email, newPassword, app_type };
if (!await window.networkAPI.sendUcMessage(codeResetPassword, messageData)) return false;
const response = await waitForResponse();
if (response && response.operationCode === codeOk) {
return true;
} else {
await window.uiAPI.showAlert(response?.metaInfo?.message || 'Error resetting password.');
return false;
}
}