added email verification

This commit is contained in:
andrei-mihnea-cerbu
2025-02-04 19:08:34 +02:00
parent 9baec7a7cb
commit dcf505c89b
67 changed files with 3922 additions and 3638 deletions
+33 -42
View File
@@ -8,10 +8,12 @@ document.addEventListener('DOMContentLoaded', async function () {
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.electronAPI.getOperationsCodes();
const operationCodes = await window.networkAPI.getOperationsCodes();
if (!operationCodes) {
await window.electronAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.');
await window.uiAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.');
return;
}
@@ -22,12 +24,12 @@ document.addEventListener('DOMContentLoaded', async function () {
resetPasswordButton.addEventListener('click', function (e) {
e.preventDefault();
window.electronAPI.changeContent('reset_password');
window.uiAPI.changeContent('reset_password');
});
signUpButton.addEventListener('click', function (e) {
e.preventDefault();
window.electronAPI.changeContent('sign_up');
window.uiAPI.changeContent('sign_up');
});
// Submit button logic (handle login)
@@ -42,50 +44,52 @@ document.addEventListener('DOMContentLoaded', async function () {
const password = formData.get('password');
// Open a TCP socket to the stored IP
if (!await window.electronAPI.openUcSocket()) {
await window.electronAPI.showAlert('Internal error of the application. Unable to open socket.');
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.electronAPI.closeUcSocket();
await window.networkAPI.closeUcSocket();
return;
}
// Fetch and store user info
if (!await fetchAndStoreUserInfo(email)) {
await window.electronAPI.closeUcSocket();
await window.networkAPI.closeUcSocket();
return;
}
// Fetch user info from local storage
const userInfo = await window.electronAPI.readUserConfig('user_info');
const userInfo = await window.databaseAPI.getUserInfo('user_info');
if (!userInfo) {
await window.electronAPI.closeUcSocket();
await window.electronAPI.showAlert('Failed to fetch user info.');
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.electronAPI.closeUcSocket();
await window.networkAPI.closeUcSocket();
return;
}
// Close the socket and navigate to main menu after success
await window.electronAPI.closeUcSocket();
await window.electronAPI.changeContent('main_menu');
await window.networkAPI.closeUcSocket();
await window.uiAPI.startWorkers();
await window.databaseAPI.setLoginStatus(true);
await window.uiAPI.changeContent('main_menu');
});
});
async function attemptLogin(email, password) {
const app_type = await window.electronAPI.readUserConfig('app_type');
const app_type = await window.databaseAPI.getAppType();
const messageData = {email, password, app_type};
// Send the login message to the server
if (!await window.electronAPI.sendUcMessage(codeLogin, messageData)) {
await window.electronAPI.showAlert('Failed to send login request.');
if (!await window.networkAPI.sendUcMessage(codeLogin, messageData)) {
await window.uiAPI.showAlert('Failed to send login request.');
return false;
}
@@ -93,57 +97,44 @@ async function attemptLogin(email, password) {
const response = await waitForResponse();
if (!response) {
await window.electronAPI.showAlert('No response from server.');
await window.uiAPI.showAlert('No response from server.');
return false;
}
if (response.operationCode !== codeOk) {
await window.electronAPI.showAlert(response.metaInfo.message);
await window.uiAPI.showAlert(response.metaInfo.message);
return false;
}
const user_info = await window.electronAPI.readUserConfig('user_info');
if (!user_info || (user_info && user_info.email !== email)) {
await window.electronAPI.resetApplicationInfo();
await window.electronAPI.resetMemory();
await window.electronAPI.resetUserConfig();
await window.electronAPI.writeUserConfig('app_type', app_type);
await window.electronAPI.writeUserConfig('user_info', {email, password});
return true;
}
return true;
}
async function fetchAndStoreUserInfo(userEmail) {
if (!await window.electronAPI.sendUcMessage(codeFindByEmail, {email: userEmail})) {
await window.electronAPI.showAlert('Failed to send request to fetch user info.');
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) {
let userInfo = await window.electronAPI.readUserConfig('user_info');
if (!userInfo) {
userInfo = {};
}
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.electronAPI.writeUserConfig('user_info', userInfo);
await window.databaseAPI.writeUserInfo(userInfo);
return true;
}
await window.electronAPI.showAlert('Failed to fetch user info from server.');
await window.uiAPI.showAlert('Failed to fetch user info from server.');
return false;
}
async function fetchAndStoreEncryptionKey(userId) {
if (!await window.electronAPI.sendUcMessage(codeFindKeyByUser, {userId})) {
await window.electronAPI.showAlert('Failed to send request to fetch encryption key.');
if (!await window.networkAPI.sendUcMessage(codeFindKeyByUser, {userId})) {
await window.uiAPI.showAlert('Failed to send request to fetch encryption key.');
return false;
}
@@ -155,10 +146,10 @@ async function fetchAndStoreEncryptionKey(userId) {
iv: response.metaInfo.key.iv,
};
await window.electronAPI.writeUserConfig('encryption_key', encryptionKey);
await window.databaseAPI.writeEncryptionKey(encryptionKey);
return true;
}
await window.electronAPI.showAlert('Failed to fetch encryption key from server.');
await window.uiAPI.showAlert('Failed to fetch encryption key from server.');
return false;
}