let codeLogin = ''; let codeFindByEmail = ''; let codeFindKeyByUser = ''; let codeOk = ''; document.addEventListener('DOMContentLoaded', async function () { const submitButton = document.getElementById('submit'); const resetPasswordButton = document.getElementById('resetPassword'); const signUpButton = document.getElementById('signup'); await window.databaseAPI.setLoginStatus(false); // Retrieve the operation codes via IPC const operationCodes = await window.networkAPI.getOperationsCodes(); if (!operationCodes) { await window.uiAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.'); return; } codeLogin = operationCodes.LOGIN; codeFindByEmail = operationCodes.FIND_BY_EMAIL; codeFindKeyByUser = operationCodes.FIND_KEY_BY_USER_ID; codeOk = operationCodes.OK; resetPasswordButton.addEventListener('click', function (e) { e.preventDefault(); window.uiAPI.changeContent('reset_password'); }); signUpButton.addEventListener('click', function (e) { e.preventDefault(); window.uiAPI.changeContent('sign_up'); }); // Submit button logic (handle login) submitButton.addEventListener('click', async function (e) { e.preventDefault(); console.log('Submit button clicked'); const form = document.getElementById('loginForm'); const formData = new FormData(form); const email = formData.get('email'); const password = formData.get('password'); // Open a TCP socket to the stored IP if (!await window.networkAPI.openUcSocket()) { await window.uiAPI.showAlert('Internal error of the application. Unable to open socket.'); return; } // Attempt login if (!await attemptLogin(email, password)) { await window.networkAPI.closeUcSocket(); return; } // Fetch and store user info if (!await fetchAndStoreUserInfo(email)) { await window.networkAPI.closeUcSocket(); return; } // Fetch user info from local storage const userInfo = await window.databaseAPI.getUserInfo('user_info'); if (!userInfo) { await window.networkAPI.closeUcSocket(); await window.uiAPI.showAlert('Failed to fetch user info.'); return; } // Fetch and store encryption key if (!await fetchAndStoreEncryptionKey(userInfo.id)) { await window.networkAPI.closeUcSocket(); return; } // Close the socket and navigate to main menu after success await window.networkAPI.closeUcSocket(); await window.workersAPI.startWorkers(); await window.databaseAPI.setLoginStatus(true); await window.uiAPI.changeContent('main_menu'); }); }); async function attemptLogin(email, password) { const app_type = await window.databaseAPI.getAppType(); const messageData = {email, password, app_type}; // Send the login message to the server if (!await window.networkAPI.sendUcMessage(codeLogin, messageData)) { await window.uiAPI.showAlert('Failed to send login request.'); return false; } // Wait for the response using waitForResponse const response = await waitForResponse(); if (!response) { await window.uiAPI.showAlert('No response from server.'); return false; } if (response.operationCode !== codeOk) { await window.uiAPI.showAlert(response.metaInfo.message); return false; } return true; } async function fetchAndStoreUserInfo(userEmail) { if (!await window.networkAPI.sendUcMessage(codeFindByEmail, {email: userEmail})) { await window.uiAPI.showAlert('Failed to send request to fetch user info.'); return false; } // Wait for the response using waitForResponse const response = await waitForResponse(); if (response && response.operationCode === codeOk) { const userInfo = {}; userInfo.id = response.metaInfo.id; userInfo.email = response.metaInfo.email; userInfo.departmentId = response.metaInfo.departmentId; userInfo.name = response.metaInfo.name || 'User'; await window.databaseAPI.writeUserInfo(userInfo); return true; } await window.uiAPI.showAlert('Failed to fetch user info from server.'); return false; } async function fetchAndStoreEncryptionKey(userId) { if (!await window.networkAPI.sendUcMessage(codeFindKeyByUser, {userId})) { await window.uiAPI.showAlert('Failed to send request to fetch encryption key.'); return false; } // Wait for the response using waitForResponse const response = await waitForResponse(); if (response && response.operationCode === codeOk) { const encryptionKey = { key: response.metaInfo.key.key, iv: response.metaInfo.key.iv, }; await window.databaseAPI.writeEncryptionKey(encryptionKey); return true; } await window.uiAPI.showAlert('Failed to fetch encryption key from server.'); return false; }